1

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) +265

MyProject.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) +146

System.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(); }

看來,當實例化MyRoleProviderHttpContext不爲空,而當叫IsInRoleHttpContext爲空。 如果我想開始一個新的生命範圍,它只會被使用一次,如果MVC啓動一個新的線程,我也會有一個新的DbContext。我很困惑找到解決方案。你有沒有? 如何爲所有後臺線程啓動新的生命範圍-I啓動它們還是MVC啓動它們?

回答

1

您的問題實際上與異步運行控制器無關,但是控制對象生命週期的一般問題。

您可能在應用程序的web.config中註冊了您的MyRoleProvider,或者可能通過代碼註冊了它。無論如何,MyRoleProvider是一個單身人士,在應用程序的整個生命週期中,這個類只有一個實例。

然而,MyRoleProvider取決於DbContext其中有一種生活方式,更短(每個Web請求或混合在你的情況下),這意味着你的MyRoleProver的壽命期間,您將無法緩存這些DbContext,因爲這將「促進'DbContext也是單身人士的生活方式。 _context = MyIoCWrapper.GetService();行似乎表明您正在緩存DbContext

在這種情況下,您將必須解決每個方法調用中的DbContext實例。相反,您的IsUserInRole看起來像這樣:

public bool IsUserInRole(String username, String roleName) 
{ 
    var context = MyIoCWrapper.GetService(); 
    return context.Roles 
     .Any(r => r.Name == roleName && r.User.Name == username); 
} 
+1

OSh !!!!是的是的你是對的。我忘了提供者是單身人士):對於一個無關的問題抱歉。我對自己很難,雖然這是一個簡單明瞭的情況 –