为了帮助提高客户端的性能,我将请求的处理转移到任务上。这样做是因为处理通常需要一些时间,而且我不希望客户端等待一段时间才得到 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
}
}
所以我的问题是当回收或网站停止时这些任务会发生什么。
当 IS 被回收时,它会等待所有线程完成并退出 - 直到池中的超时值为止。超时后,它们会杀死所有正在运行的线程并重新开始。
因此,您可以向线程设置一个信号,以便在应用程序请求关闭时停止globa.asax
使用Application_End
功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)