Why does MVC 4 control the controller when something is waiting?

Why is my controller located after the await instruction?

  public async Task<ViewResult> MyAction() { await Task.Yield(); // controller disposed at this point... why? return this.View(); } 

My controller uses the resources it has, overriding the Dispose method ... but using async / await seems to violate this because it controls the controller as soon as the wait statement is executed ... how can I manage the resources so that async is supported / await?

EDIT: Recent methods in the stop gloss of the dispose method:

 MyWebRole.dll!MyWebRole.Code.MyController.Dispose(bool disposing = true) Line 102 C# System.Web.Mvc.dll!System.Web.Mvc.Controller.Dispose() + 0x25 bytes System.Web.Mvc.dll!System.Web.Mvc.DefaultControllerFactory.ReleaseController(System.Web.Mvc.IController controller = {MyWebRole.Areas.App.Controllers.LongPollingController}) + 0x3e bytes System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.BeginProcessRequest.AnonymousMethod__5() + 0x70 bytes System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.MakeVoidDelegate.AnonymousMethod__0() + 0x2d bytes System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.BeginSynchronous<System.Web.Mvc.Async.AsyncVoid>.AnonymousMethod__7(System.IAsyncResult _ = {System.Web.Mvc.Async.SimpleAsyncResult}) + 0x2b bytes System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.End() + 0x99 bytes System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End<System.Web.Mvc.Async.AsyncVoid>(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>}, object tag = {object}) + 0x3c bytes System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.End(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>}, object tag = {object}) + 0x29 bytes System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.EndProcessRequest.AnonymousMethod__d() + 0x27 bytes System.Web.Mvc.dll!System.Web.Mvc.SecurityUtil.GetCallInAppTrustThunk.AnonymousMethod__0(System.Action f = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0x20 bytes System.Web.Mvc.dll!System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(System.Action action = {Method = {System.Reflection.RuntimeMethodInfo}}) + 0x3e bytes System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult asyncResult = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>}) + 0x77 bytes System.Web.Mvc.dll!System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult result = {System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>}) + 0x27 bytes 

The Dispose method is called with disposing = true ... this is bad. Maybe a mistake?

EDIT 2: I tried subclassing AsyncController , but the same thing happens.

Any work around?

+4
source share
3 answers

The call stack shown in the question refers to MVC3, not MVC4. Unfortunately, MVC3 does not support asynchronous calls based on Task . When you call the wait, the task can be started, but MVC3 knows nothing about it, so it just calls ToString() and returns it as ContentResult .

Task based async support was added in MVC4, and actually no output from AsyncController is actually required. All Controllers in sync support for MVC4 and Task based on async. You only need the AsyncController if you are using the Async / Completed template.

+6
source

Based on the stack trace, it seems that no matter what started with the process request, it stopped it, so you lost the link to the controller and got the right to collect / delete garbage. I would look at what started the process request and why it would end it.

0
source

I had this problem and it bothered me all day. Finally, I realized that the ActionInvoker I connected in my ControllerFactory inherits from ControllerActionInvoker. When I changed this to AsyncControllerActionInvoker, the problem disappeared.

0
source

All Articles