AsyncController = nice. How about AsyncActionResult?
-
Tuesday, March 27, 2012 2:35 AM
Hi all,
I have an Async/MVC query...
AsyncController class is all fine and dandy however I have a use-case where I need an asynchronous ActionResult - that means in the ExecuteResult method I have a potentially long running operation with blocking I/O (it's an ActionResult designed to download some data)
In this case the data could be rather large so the idea is to support partial download and I would like to write the streaming code using non-blocking async methods.
So what I would like is a class like this;
public class AsyncActionResult : ActionResult { public override void ExecuteResult(ControllerContext context) { // According to the deep dive session @ build - this is wrong! ExecuteResultAsync(context).Wait(); } public virtual Task ExecuteResultAsync(ControllerContext context) { // Do your async stuff here } }
So my question is this; what is the best way to implement AsyncActionResult or is there a better method for doing this?!
Regards,
Adrian
If it ain't broke - break it!
All Replies
-
Wednesday, March 28, 2012 4:31 PM
Hi Adrian -
Unfortunately MVC does not support asynchronous ActionResults; only asynchronous Controllers. However, you do have a few options available to you for this scenario:
- If you're sending a file from the local hard disk to the client, use Response.TransmitFile in your ActionResult. If you're running IIS7+, this code path is highly optimized, and you should get very good performance. The call to TransmitFile should return immediately; the file will be transmitted by IIS at the end of the request after ASP.NET has finished processing the request.
- Put the asynchronous code in your Controller instead of your ActionResult. It might be a little unclean, but it should work, since Controllers can be truly asynchronous.
- Consider Web API for this particular functionality. The programming model is very similar to MVC, all parts of it are async all the way down, and Web API controllers can coexist happily with MVC controllers in the same application.
Hope this helps!
- Proposed As Answer by Stephen Toub - MSFTMicrosoft Employee, Moderator Wednesday, March 28, 2012 4:32 PM
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Moderator Friday, March 30, 2012 1:53 PM
-
Wednesday, March 28, 2012 11:26 PM
4) Implement AsyncActionResult, modify the ASP.NET MVC framework so that it's aware of it (and will properly execute it asynchronously), and submit a pull request.
OK, it's probably infeasable, but it is an option. Which is really cool.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible

