title: SimpleInjector and System.Web.Mvc.Async threads
body: <p>I <a href="https://stackoverflow.com/questions/13147509/simpleinjector-mixed-lifestyle-for-per-thread-and-per-webrequest">asked before a question</a> here, and I read this question/answers about multi-threading and I know those solutions. But today I get a new problem. When we are using commands (or where we can access the original code to manage and modify it) the async-decorator that suggested in above answers works. But when MVC creates a new thread itself, what can we do? e.g. I have a custom role provider (works with <code>DbContext</code>), and I get this error:</p>
  
  <blockquote>
    <p>The operation cannot be completed because the DbContext has been disposed. </p>
  </blockquote>
  
  <p>And here is stack trace:</p>
  
  <blockquote>
    <p>[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]</p>
    
    <p>System.Data.Entity.Internal.InternalContext.CheckContextNotDisposed() +67</p>
    
    <p>System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +34</p>
    
    <p>System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +22</p>
    
    <p>System.Linq.Queryable.Any(IQueryable<code>1 source, Expression</code>1 predicate) +265</p>
    
    <p><strong><em>MyProject.MyRoleProvider.IsUserInRole(String username, String roleName) in ...</em></strong></p>
    
    <p>System.Web.Security.Roles.IsUserInRole(String username, String roleName) +263</p>
    
    <p>MyProject.MyPrincipal.IsInRole(String role) in ...</p>
    
    <p>System.Linq.Enumerable.Any(IEnumerable<code>1 source, Func</code>2 predicate) +146</p>
    
    <p>System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +200</p>
    
    <p>System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +159</p>
    
    <p>System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +96</p>
    
    <p>System.Web.Mvc.Async.<strong><em>&lt;>c__DisplayClass25.b__1e</em></strong>(AsyncCallback asyncCallback, Object asyncState) +446</p>
    
    <p>System.Web.Mvc.Async.<strong><em>WrappedAsyncResult`1.Begin</em></strong>(AsyncCallback callback, Object state, Int32 timeout) +130</p>
    
    <p>System.Web.Mvc.Async.<strong><em>AsyncControllerActionInvoker.BeginInvokeAction</em></strong>(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302</p>
    
    <p>System.Web.Mvc.<strong><em>&lt;>c__DisplayClass1d.b__17</em></strong>(AsyncCallback asyncCallback, Object asyncState) +30</p>
    
    <p>System.Web.Mvc.Async.<strong><em>WrappedAsyncResult`1.Begin</em></strong>(AsyncCallback callback, Object state, Int32 timeout) +130</p>
    
    <p>System.Web.Mvc.<strong><em>Controller.BeginExecuteCore</em></strong>(AsyncCallback callback, Object state) +382</p>
    
    <p>System.Web.Mvc.Async.<strong><em>WrappedAsyncResult`1.Begin</em></strong>(AsyncCallback callback, Object state, Int32 timeout) +130</p>
    
    <p>System.Web.Mvc.<strong><em>Controller.BeginExecute</em></strong>(RequestContext requestContext, AsyncCallback callback, Object state) +317</p>
    
    <p>System.Web.Mvc.Controller.System.Web.Mvc.Async.<strong><em>IAsyncController.BeginExecute</em></strong>(RequestContext requestContext, AsyncCallback callback, Object state) +15</p>
    
    <p>System.Web.Mvc.<strong><em>&lt;>c__DisplayClass8.b__2</em></strong>(AsyncCallback asyncCallback, Object asyncState) +71</p>
    
    <p>System.Web.Mvc.Async.<strong><em>WrappedAsyncResult`1.Begin</em></strong>(AsyncCallback callback, Object state, Int32 timeout) +130</p>
    
    <p>System.Web.Mvc.<strong><em>MvcHandler.BeginProcessRequest</em></strong>(HttpContextBase httpContext, AsyncCallback callback, Object state) +249</p>
    
    <p>System.Web.Mvc.<strong><em>MvcHandler.BeginProcessRequest</em></strong>(HttpContext httpContext, AsyncCallback callback, Object state) +50</p>
    
    <p>System.Web.Mvc.MvcHandler.System.Web.<strong><em>IHttpAsyncHandler.BeginProcessRequest</em></strong>(HttpContext context, AsyncCallback cb, Object extraData) +16</p>
    
    <p>System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301</p>
    
    <p>System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155</p>
  </blockquote>
  
  <p>As you can see, <code>MyProject.MyRoleProvider.IsUserInRole</code> invoked asynchronously, which I did not start it and its invoking async by MVC itself. So I have not any control on it. My provider ctor is:</p>
  
  <p>public MyRoleProvider() {
      _context = MyIoCWrapper.GetService();
  }</p>
  
  <p>It seems when <code>MyRoleProvider</code> instantiated, <code>HttpContext</code> is not null, and when <code>IsInRole</code> called, <code>HttpContext</code> is null.
  If I want to start a new life scope, it will be used just once, and if MVC starts a new thread, I will have a new <code>DbContext</code> too. I confused to finding a solution. Have you any one?
  How can I start a new life scope for all background threads -I start them or MVC starts them?</p>