开发者

ASP.NET MVC 2 Cancel Asynchronous Action

The MSDN documentation for using an asynchronous controller mentions choosing an asynchronous controller when you want to provide a way for users to cancel requests.

I couldn't find any documentation related to actually allowing users to cancel asynchronous requests, however. Is there a way to indicate to ASP.NET and/or the ASP开发者_Go百科.NET MVC framework that a request should be canceled?


If the asynchronous API that you called provides a way to cancel the request (either via .NET 4's CancellationToken class or via a CancelAsync method), you must call that to cancel whatever asynchronous operations are in flight. Otherwise, call AsyncManager.Finish (via the controller's AsyncManager property) to force the MVC framework to call your XxCompleted method immediately. Note that AsyncManager.Finish is really intended only if you need to bail out of requests that you have no other way of canceling (such as calling BeginXx / EndXx without a CancellationToken) and won't always work in other scenarios.


The default timeout is 45 seconds, so if there is no response, it will be canceled by server. They did not allow users to cancel the request as to cancel one http request, you are creating another http request which is not a proper way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜