title: How can i access IServiceCollection from a background thread
body: <p>I am trying to perform some long running operations in the background. These operations would need access to some services that have been registered with ServiceCollection in Startup.cs.</p>
  
  <p>I am injecting IServiceProvider to the class that is going to spawn a background thread. </p>
  
  <pre><code>public WorkflowService(IServiceProvider serviceProvider)
  {
      _serviceProvider = serviceProvider;
  }
  </code></pre>
  
  <p>(<em>Note, i cannot inject individual services because i don't know before-hand which component is going to be invoked and which services each component will require</em>).</p>
  
  <p>I am then passing _serviceProvider to the method that will be executed in the background.</p>
  
  <pre><code>Task task = new Task(() =&gt; ExecuteStep(firstStep, workflowInstanceId, workflowMetadata, _serviceProvider));
  task.Start();
  </code></pre>
  
  <p>Ths issue is that by the time i try to resolve a service on the background thread, IServiceProvider has been disposed and i get an exception.</p>
  
  <pre><code>object service = _serviceProvider.GetRequiredService(item.GetType());
  </code></pre>
  
  <p>In ASP.NET 4.5, i could have achieved something similar by using GlobalConfiguration.Configuration.DependencyResolver, but i am unable to find something equivalent in Core 2.2 or figure out the right way to achieve this.</p>
  
  <pre><code>using (var scope = GlobalConfiguration.Configuration.DependencyResolver.BeginScope())
  {
  
  ....
       //Obtain the Type from the DI container
       object service = scope.GetService(serviceType);
  ....
  }
  </code></pre>