根据我的所有经验,无论是作为经典 ASP 还是 ASP.NET 开发人员,我一直都理解设置 Server.ScriptTimeout
的调用。 value 在当前请求的范围内是本地的。换句话说,调用 Server.ScriptTimeout = 600
会将当前请求的处理时间设置为 10 分钟。对其他资源的后续甚至并发请求将使用 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 似乎对这个问题保持沉默。任何链接和/或经验 - 一种或另一种方式 - 将不胜感激!涵盖这方面的文档似乎很少,我将不胜感激任何权威信息。
最佳答案
它确实是特定于请求的:
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);
}
}
在哪里
_context
是 HttpContext
关于asp.net - Server.ScriptTimeout 在范围内设置全局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917317/