为了帮助提高客户端的性能,我将请求的处理卸载到任务上。这样做是因为处理通常需要一些时间,而且我不希望客户端等待一段时间才能获得 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 回收或网站停止时这些任务会发生什么。