I asked before a question這裏,我讀了關於多線程的這個問題/答案,我知道這些解決方案。但今天我遇到了一個新問題。當我們使用命令(或者我們可以訪問原始代碼來管理和修改它)時,上面的答案中提出的async-decorator就可以工作。但是當MVC本身創建一個新線程時,我們能做什麼?例如我有一個自定義角色提供(與DbContext
作品),我得到這個錯誤:SimpleInjector和System.Web.Mvc.Async線程
The operation cannot be completed because the DbContext has been disposed.
這裏是堆棧跟蹤:
[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]
System.Data.Entity.Internal.InternalContext.CheckContextNotDisposed() +67
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +34
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +22
System.Linq.Queryable.Any(IQueryable
1 source, Expression
1 predicate) +265MyProject.MyRoleProvider.IsUserInRole(String username, String roleName) in ...
System.Web.Security.Roles.IsUserInRole(String username, String roleName) +263
MyProject.MyPrincipal.IsInRole(String role) in ...
System.Linq.Enumerable.Any(IEnumerable
1 source, Func
2 predicate) +146System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +200
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +159
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +96
System.Web.Mvc.Async.<>c__DisplayClass25.b__1e(AsyncCallback asyncCallback, Object asyncState) +446
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
System.Web.Mvc.<>c__DisplayClass1d.b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
System.Web.Mvc.<>c__DisplayClass8.b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
正如你所看到的,MyProject.MyRoleProvider.IsUserInRole
異步調用的,我做到了不是啓動它,而是通過MVC本身調用異步。所以我沒有任何控制權。我的提供商ctor是:
public MyRoleProvider(){ _context = MyIoCWrapper.GetService(); }
看來,當實例化MyRoleProvider
,HttpContext
不爲空,而當叫IsInRole
,HttpContext
爲空。 如果我想開始一個新的生命範圍,它只會被使用一次,如果MVC啓動一個新的線程,我也會有一個新的DbContext
。我很困惑找到解決方案。你有沒有? 如何爲所有後臺線程啓動新的生命範圍-I啓動它們還是MVC啓動它們?
OSh !!!!是的是的你是對的。我忘了提供者是單身人士):對於一個無關的問題抱歉。我對自己很難,雖然這是一個簡單明瞭的情況 –