在ASP.NET MVC的异步控制器中,有没有办法判断客户端是否/何时中止请求?
[NoAsyncTimeout]
public void IndexAsync() {
var source = new CancellationTokenSource();
Task.Factory.StartNew(() => {
while (true) {
if (source.Token.IsCancellationRequested) {
AsyncManager.Finish();
return;
}
Response.Write(".");
Thread.Sleep(1000);
}
}, source.Token);
// Is there any way to do this?
Request.Aborted += (sender, e) => source.Cancel();
AsyncManager.OutstandingOperations.Increment();
}
答案 0 :(得分:8)
答案 1 :(得分:2)
尝试
CancellationToken clientDisconnectedToken = HttpContext.Response.ClientDisconnectedToken;