2012-08-11 139 views
7

在我所有的經驗中,無論是作爲傳統ASP還是ASP.NET發佈者,我一直都明白,設置Server.ScriptTimeout值的調用在當前請求的範圍內是本地的。換句話說,調用Server.ScriptTimeout = 600會將當前請求的處理時間設置爲10分鐘。後續甚至是對其他資源的併發請求將使用Server.ScriptTimeout的默認設置。Server.ScriptTimeout設置全局範圍?

近日在代碼審查,我被告知,設置Server.ScriptTimeout的值設置爲頁面在網站的處理時間,直到應用程序池被回收。建議的「修復」是類似以下內容:

public class MyPage : Page { 
    private const int desiredTimeout = 600; 
    private int cachedTimeout; 

    private void Page_Load(object sender, EventArgs e) { 
    // cache the current timeout in a private store. 
    cachedTimeout = Server.ScriptTimeout; 
    Server.ScriptTimeout = desiredTimeout; 
    } 

    private void Page_Unload(object sender, EventArgs e) { 
    // restore the previous setting for the timeout 
    Server.ScriptTimeout = cachedTimeout; 
    } 
} 

這似乎很奇怪,我,作爲一名開發人員在一個頁面中調用Server.ScriptTimeout = 1可能搞垮網站其他每個頁面將只允許一秒鐘處理。此外,這種行爲會影響當前Page_Load和Page_Unload事件之間可能發生的任何當前請求 - 這看起來像是一個併發的噩夢。

要徹底,但是,我做了一個測試工具由兩頁 - ,設置Server.ScriptTimeout一些真正的高數和第二頁,僅僅顯示Server.ScriptTimeout當前值。無論我在上設置了什麼值,頁面第二頁總是顯示默認值。所以,我的測試似乎證實Server.ScriptTimeout在本地範圍內。

我確實注意到如果我的web.config的debug =「true」,Server.ScriptTimeout不起作用 - 而MSDN在其頁面上明確聲明瞭這一點。在這種模式下,所有讀取Server.ScriptTimeout的值的呼叫都會返回一個非常大的數字,無論我設置爲什麼。

所以我的問題是,和絕對確保我不缺少的東西,有一個實例,它設置爲Server.ScriptTimeout值影響的整個網站(全球範圍)的處理時間,或者是我的信仰有效,只有當地情況?我已經谷歌搜索這個問題無濟於事,MSDN似乎在這個問題上保持沉默。

任何鏈接和/或經驗 - 這樣或那樣 - 將不勝感激!涵蓋這方面的文件似乎很少,我希望得到任何權威信息。

回答

9

這確實是請求特定的:

public int ScriptTimeout 
{ 
    get 
    { 
     if (this._context != null) 
     { 
      return Convert.ToInt32(this._context.Timeout.TotalSeconds, CultureInfo.InvariantCulture); 
     } 
     return 110; 
    } 
    [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Medium)] 
    set 
    { 
     if (this._context == null) 
     { 
      throw new HttpException(SR.GetString("Server_not_available")); 
     } 
     if (value <= 0) 
     { 
      throw new ArgumentOutOfRangeException("value"); 
     } 
     this._context.Timeout = new TimeSpan(0, 0, value); 
    } 
} 

其中_contextHttpContext

+0

其中從該代碼示例?感謝你的回答! – BradBrening 2012-08-11 21:43:49

+2

@BradBrening反射器是你的朋友:-) – twoflower 2012-08-12 06:19:21

+0

我明白了。再次感謝,它沒有比對運行代碼的實際檢查更具有批判性。 – BradBrening 2012-08-12 12:00:59