gpt4 book ai didi

asp.net - 请求超时。 Web 服务器未能在指定时间内响应

转载 作者:行者123 更新时间:2023-12-02 16:22:52 25 4
gpt4 key购买 nike

我在 Azure 应用服务中托管了一个 ASP .NET 网络应用。 Web 应用程序的功能是从 excel 中读取数据并匹配 Azure SQL 数据库中的数据并通过 API 发布数据。该应用程序运行良好,但问题是它不会运行更长时间,它会抛出类似

的错误
500 - The request timed out.
The web server failed to respond within the specified time.

我还增加了 Web.Config 文件中的执行时间限制。

<system.web>
<customErrors mode="Off" />
<compilation targetFramework="4.6.1" />
<httpRuntime maxRequestLength="2097152" executionTimeout="300000" targetFramework="4.6.1" />
</system.web>

任何人都可以在这里帮助我做些什么吗?我可以获得至少 1 小时的执行时间吗?

最佳答案

出现这个问题是因为浏览器。你的代码没有问题,不需要修改httpRuntime

浏览器默认有连接超时时间,Chrome好像是5分钟左右,实际测试显示500-The request timed out. 4分钟左右。

测试代码

public int test()
{
try
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = dt1.AddMinutes(10);
while (DateTime.Now < dt2)
{
Thread.Sleep(1000);
Tick("I am running");
}
return 1;
}
catch (Exception)
{
return -1;
throw;
}
}

public void Tick(object data)
{
_logger.LogInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + data);
}

现象

enter image description here

大概4分钟左右,浏览器出现500超时错误,但是发现后台还在运行,日志还在记录。

enter image description here

建议

如果后端需要处理一个小时才能返回结果,则必须异步返回结果。同步是不可能的,没有理由同步,即使浏览器不超时,你也不需要同步返回,浪费资源。

要及时返回处理结果,可以使用WebSocket和Ajax轮询,或者signalr。

在处理过程中,前端只需要提供Loading提示,然后定时轮询查询后端处理结果即可。如果处理成功,则前端提示更新成功。

关于asp.net - 请求超时。 Web 服务器未能在指定时间内响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65177544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com