为了帮助提高客户端的性能,我将请求的处理卸载到任务上。这样做是因为处理通常需要一些时间,而且我不希望客户端等待一段时间只是为了获得 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/