title: Autofac Creating Child Scope for New Thread not working as expected "Instances cannot be resolved and nested lifetimes cannot be created..."
body: <p>When someone calls the URL "/index/5", I want to return a view, and in parallel I want to start a new thread in which I want to figure out with some DB calls and business logic whether I should send someone else a notification. Here is a simplified representation of my setup, which gives an error.</p>
  
  <p>How can I get my child scope to work in a parallel thread?</p>
  
  <p><strong>App Startup</strong></p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterType&lt;MyRepository&gt;().As&lt;IMyRepository();
  builder.RegisterType&lt;Entities&gt;().As&lt;Entities&gt;().InstancePerRequest();
  var container = builder.Build();
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    private readonly IMyRepository _myRepository ;
  
      public MyController(
          IMyRepository myRepository
         )
      {
          _myRepository = myRepository;
      }
  
      public async Task&lt;ActionResult&gt; Index(int id)
      {
          _myRepository.DoSomething(id);
  
          return View();
      }
  </code></pre>
  
  <p><strong>Repository:</strong></p>
  
  <pre><code>private ILifetimeScope _lifeTimeScopeChild = null;
  
  public void DoSomething(int id){
              //start new thread with child scope
              using(var threadLifeTime = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope())
              {
                  _lifeTimeScopeChild = threadLifeTime;
                   Thread t = new Thread(new ParameterizedThreadStart(MySeparateThread));
                   t.Start(id);  
              }
          }
  
          private void MySeparateThread(object id) {
                      var _entities = _lifeTimeScopeChild.Resolve&lt;Entities&gt;(); //IT CRASHES HERE
          }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
  </code></pre>
  
  <p>What I'm trying to accomplish: <a href="https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#thread-scope" rel="nofollow noreferrer">https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#thread-scope</a></p>