asp.net - Server.ScriptTimeout 在范围内设置全局?

标签 asp.net

根据我的所有经验,无论是作为经典 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);
    }
}

在哪里 _contextHttpContext

关于asp.net - Server.ScriptTimeout 在范围内设置全局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917317/

相关文章:

c# - 如何计算 Nullable<T> 数据类型的大小

c# - 哪种向 ASP.NET Dictionary 类添加项目的方法效率更高?

c# - 使用参数运行 ASP.NET 页面?

javascript - 使用jquery和jsonp的ASP.NET跨域Web服务调用错误

asp.net - 强制 SSL 连接

asp.net - 如何增加 ASP.NET Ajax PageMethod 的超时时间?

asp.net - 在默认的 ASP.NET Core 项目中,为什么 "Forgot password"功能会两次请求用户电子邮件?

c# - span 标签 onclick 调用代码隐藏方法

javascript - 当我将鼠标悬停在“产品”菜单上时,此代码需要进行哪些更改以提供正确的外观?

c# - 为什么我的 SQL 查询返回重复的结果?