为什么我的 Controller 在 await
语句之后被释放?
public async Task<ViewResult> MyAction()
{
await Task.Yield();
// controller disposed at this point... why?
return this.View();
}
我的 Controller 使用它通过重写 Dispose
方法处理的资源...但是使用 async/await 似乎打破了这一点,因为它会在执行 await 语句后立即处理 Controller 。 . 如何以支持异步/等待的方式处理资源?
编辑: dispose 方法调用栈中的最后一个方法:
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
正在使用 disposing = true
调用 Dispose
方法……这很糟糕。也许是一个错误?
编辑 2: 我已经尝试子类化 AsyncController
但同样的事情发生了。
有解决办法吗?
最佳答案
问题中显示的调用堆栈来自 MVC3 而不是 MVC4。不幸的是,MVC3 不支持基于Task
的异步调用。当您调用 await 时,任务很可能会启动,但 MVC3 对此一无所知,因此它只是对其调用 ToString()
并将其作为 ContentResult
返回.
在 MVC4 中添加了基于 Task
的异步支持,实际上甚至不需要从 AsyncController
派生。 MVC4 中的所有 Controllers
都支持同步,以及基于 Task
的异步。如果您使用 Async/Completed 模式,则只需要 AsyncController
。
关于c# - 为什么 MVC 4 在等待操作时处理 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561985/