c# - IIS 回收时运行的任务会发生什么

标签 c# asp.net c#-4.0 iis iis-7.5

为了帮助提高客户端的性能,我将请求的处理卸载到任务上。这样做是因为处理通常需要一些时间,而且我不希望客户端等待一段时间只是为了获得 200 响应。将工作卸载到任务上的 Web 服务始终在处理帖子。

public void ProcessRequest(HttpContext context)
{
    // check for bad requests -- return 400

    // get copy of the context input stream

    Task.Factory.StartNew(() =>
    {
        ProcessRequest(contextInputStreamCopy);
    });
}

private void ProcessRequest(Stream inputStream)
{
    try
    {
        // process input stream
    }
    catch(Exception ex)
    {
        // any server error that would normally result in 500 response are not
        // exposed to the clients, the clients are to see 200 when the server 
        // encounters an error
    }
}

所以我的问题是,当 IIS 回收或网站停止时,这些任务会发生什么情况。

最佳答案

当 IIS 回收时,它会等待所有线程完成并退出 - 直到池中的超时值。超时后,他们会杀死所有正在运行的线程并重新开始。

因此,当应用程序在 globa.asax 处使用 Application_End 函数请求关闭时,您可以向线程设置一个停止信号。

关于c# - IIS 回收时运行的任务会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14526983/

相关文章:

c# - 如何在不使用 Rx 框架的情况下限制事件的速度

c# - jquery "async = true"调用.net web 服务不异步工作

c# - 在 C# 中动态覆盖任何方法

c# - 是否可以在 XP 上运行 .NET 4.5 应用程序?

c# - 如何从datagridview和数据库中删除选定的行

c# - 在 ASP.net 中使用资源作为 HTML 元素的属性

c# - 访问修改后的闭包 : ReSharper

c# - 使用 StringBuilder 编写语句的更简单方法

c# - UInt32.TryParse() 十六进制数不工作

javascript - 如何在 ASP.NET 母版页中正确引用 javascript 文件?