You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure App Service部署的ASP.NET能否修改默认504网关超时时间?

解决方案:调整Azure App Service与ASP.NET应用的超时配置以支持长耗时同步请求

要解决Azure App Service上ASP.NET应用的504网关超时问题,需要从Azure平台配置、ASP.NET应用内部配置和前端AJAX配置三个层面逐一调整,确保整个请求链路的超时时间覆盖你的最长耗时操作(10分钟以内)。

一、Azure App Service 平台层面配置

Azure App Service默认请求超时为2分钟(120秒),需先调整至平台允许的最大值230秒(约3分50秒)——这是平台硬限制,若你的操作超过该时长,后续需结合应用层异步处理+前端轮询(你明确要求同步调用,优先配置到平台上限即可):

  • 登录Azure门户,进入目标App Service资源
  • 左侧菜单选「配置」→「常规设置」
  • 在「请求超时」项中填入230(单位:秒),保存更改

二、ASP.NET应用内部配置

根据你使用的ASP.NET版本,调整应用自身超时参数:

1. ASP.NET Core(3.x及以上)

在Program.cs中配置Kestrel服务器超时,并确保HTTP上下文超时匹配:

var builder = WebApplication.CreateBuilder(args);

// 配置Kestrel超时参数
builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10);
    options.Limits.RequestTimeout = TimeSpan.FromMinutes(10);
});

// 其他服务配置...

var app = builder.Build();

// 覆盖HTTP上下文超时(确保覆盖请求全流程)
app.Use(async (context, next) =>
{
    context.RequestAborted = new CancellationTokenSource(TimeSpan.FromMinutes(10)).Token;
    await next();
});

// 中间件、路由等配置...

app.Run();

若通过IIS部署(Windows App Service),需在web.config中补充:

<system.web>
  <!-- 兼容ASP.NET Framework配置,纯Core项目可保留 -->
  <httpRuntime executionTimeout="600" maxRequestLength="1048576" />
</system.web>
<aspNetCore processPath="dotnet" arguments=".\YourApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" requestTimeout="00:10:00" />

2. ASP.NET Framework

直接在web.config中修改关键配置:

<system.web>
  <!-- 设置请求执行超时为10分钟 -->
  <httpRuntime executionTimeout="600" maxRequestLength="1048576" />
</system.web>
<system.webServer>
  <handlers>
    <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
  </handlers>
  <!-- 配置ASP.NET Core模块请求超时 -->
  <aspNetCore processPath="dotnet" arguments=".\YourApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" requestTimeout="00:10:00" />
</system.webServer>

三、前端AJAX配置

前端AJAX默认超时远小于10分钟,需手动设置超时时间:

1. jQuery AJAX示例

$.ajax({
    url: '/YourController/YourLongRunningAction',
    type: 'POST',
    data: yourRequestData,
    timeout: 600000, // 10分钟,单位:毫秒
    success: function(result) {
        // 处理返回结果
        console.log(result);
    },
    error: function(xhr, status, error) {
        // 处理错误逻辑
        console.error(status, error);
    }
});

2. 原生Fetch API示例

fetch('/YourController/YourLongRunningAction', {
    method: 'POST',
    body: JSON.stringify(yourRequestData),
    headers: {
        'Content-Type': 'application/json'
    },
    signal: AbortSignal.timeout(600000) // 10分钟超时
})
.then(response => response.json())
.then(result => {
    // 处理结果
    console.log(result);
})
.catch(error => {
    // 处理超时或其他错误
    console.error(error);
});

关键注意事项

  • Azure App Service的免费/共享层有额外资源限制,长时间请求可能被强制终止,建议使用标准层及以上服务计划
  • 若操作超过平台230秒上限,即使配置应用层超时仍可能触发网关超时,此时可折中采用「控制器先返回接受状态+前端轮询结果接口」的方案
  • 长耗时操作会占用App Service工作进程,高并发场景下建议添加限流逻辑

内容的提问来源于stack exchange,提问作者Hawos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 01:25:24