2012-08-11 138 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