Azure 应用服务在 ApplicationHost.config 中设置自定义 ConnectionTimeout

Posted

技术标签:

【中文标题】Azure 应用服务在 ApplicationHost.config 中设置自定义 ConnectionTimeout【英文标题】:Azure App Service Set Custom ConnectionTimeout in ApplicationHost.config 【发布时间】:2016-08-12 19:51:02 【问题描述】:

在托管在 Azure 应用服务上的 ASP.NET MVC 网站上,我们希望对超过 15 秒的请求强制超时。这是我们一直在测试的一个总是需要超过 15 秒(即无限循环)的简单操作...

    public ActionResult TimeoutTest()
    
        var i = 1;
        while (true)
        
            i++;
        
        return new HttpStatusCodeResult(200);
    

默认情况下,如果我在浏览器中对该操作执行 GET,我将在两分钟后收到“500 - 请求超时”错误,这与 the "connectionTimeout" default setting in the webLimits section of ApplicationHost.config 一致。

所以...除非我弄错了,否则将这个 connectionTimeout 值更改为 15 秒就足够了。为此,我了解需要对 ApplicationHost.config 文件 (XDT) 使用基于转换的方法,如 here 所述。

我使用以下 applicationHost.xdt 文件执行此操作...

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.applicationHost>
      <webLimits xdt:Transform="SetAttributes(connectionTimeout)" connectionTimeout="00:00:15"/>
</system.applicationHost>
</configuration>

...之后我将文件添加到正确的位置 (d:/home/site/applicationHost.xdt)。我重新启动了我的站点,并在日志中看到转换已成功应用:

2016-04-20T08:40:44 Start 'site' site extension transform
2016-04-20T08:40:44 StartSection Executing SetAttributes (transform line 4, 18)
2016-04-20T08:40:44 on /configuration/system.applicationHost/webLimits
2016-04-20T08:40:44 Applying to 'webLimits' element (no source line info)
2016-04-20T08:40:44 Set 'connectionTimeout' attribute
2016-04-20T08:40:44 Set 1 attributes
2016-04-20T08:40:44 EndSection Done executing SetAttributes
2016-04-20T08:40:44 Successful 'D:\home\site\applicationHost.xdt' site extension transform
2016-04-20T08:40:44 sandboxproc.exe complete successfully. Ellapsed = 316.00 ms

[编辑]:我也直接在转换后检查了applicationhost.config,新的值在那里:

    ...
    </sites>
    <webLimits connectionTimeout="00:00:15" />
  </system.applicationHost>
  <system.webServer>
    <asp>
    ...

尽管如此,如果我再次点击上面的操作方法,它仍然会在两分钟后超时,而不是 15 秒。

我的问题:有人知道为什么不遵守这个超时设置吗?

我知道this post,看起来它采用了完全相同的方法(并且似乎有效?)。

【问题讨论】:

【参考方案1】:

通过 Web.Config 改用 executionTimeout 怎么样?

<system.web>
    <httpRuntime executionTimeout="30" />
    <compilation debug="false" />
</system.web>

对于 MVC 项目,您应该添加以下代码以强制将值应用于请求:

System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);

如果您希望每个请求都遵守此设置,您可以创建一个操作过滤器:

public class Timeoutter : ActionFilterAttribute

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    
        System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);
        base.OnActionExecuting(filterContext);
    

在Global.asax中调用的RegisterGlobalFilters方法中添加注册:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)

    filters.Add(new Timeoutter());
    filters.Add(new HandleErrorAttribute());

请看:

https://msdn.microsoft.com/en-us/library/e1f13641(v=vs.85).aspx http://forums.asp.net/p/1715081/4723882.aspx?Re+web+config+executionTimeout+not+working+in+ASP+NET+MVC .NET Execution Timeout Not Taking Effect in an MVC Web Project

【讨论】:

谢谢,是的,我也知道这一点。它也设置为 15 秒,似乎也没有什么区别。 我在上面添加了更多细节。

以上是关于Azure 应用服务在 ApplicationHost.config 中设置自定义 ConnectionTimeout的主要内容,如果未能解决你的问题,请参考以下文章

Azure 应用服务Python fastapi Function在Azure

将 Azure 移动服务迁移到 Azure 移动应用:UserId 已更改

将 Azure 托管标识用于部署到 Azure 的应用程序?

利用Azure存储同步服务构建垮分支机构的文件服务器资料同步

如何在 c# 中为 SQL Azure 启用内部 Azure 服务

在本地测试 Azure 服务总线,无需任何订阅或登录