title: ASP.NET Core 3.0 - Cannot access a disposed object. Object name: 'IServiceProvider'
body: <p>We have windows service that hosts web api and does some calculation job in background.
  From time to time we get exception:</p>
  
  <pre><code>2019-12-27 11:49:15.525 +01:00 [Error] [Serilog.Log.Write] Unhandled exception caught. Runtime is terminating : True
  System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'IServiceProvider'.
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
     at MediatR.ServiceFactoryExtensions.GetInstances[T](ServiceFactory factory)
     at MediatR.Internal.RequestHandlerWrapperImpl`2.Handle(IRequest`1 request, CancellationToken cancellationToken, ServiceFactory serviceFactory)
     at MediatR.Mediator.Send[TResponse](IRequest`1 request, CancellationToken cancellationToken)
     at DataProcessor.Enricher.ChangeTracking.LegacyDataChangeTracker.SendForEnrichment(TurbineTimeCounters turbineTimeCounters, Unit unit)
     at DataProcessor.Enricher.ChangeTracking.LegacyDataChangeTracker.&lt;ProcessEventsAndHistData&gt;b__29_1(Unit unit)
     at System.Threading.Tasks.Task.&lt;&gt;c.&lt;ThrowAsync&gt;b__139_1(Object state)
     at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
  </code></pre>
  
  <p>We don't have any exceptions from built in DI on startup, thus dependencies are configured correctly.</p>
  
  <p>I made investigation. MediatR's ServiceFactory is a delegate to <code>Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService</code>
   method.</p>
  
  <p><code>ServiceProviderEngineScope</code> is disposed at the time, when MediatR call's for it.
  <a href="https://github.com/aspnet/DependencyInjection/blob/master/src/DI/ServiceLookup/ServiceProviderEngineScope.cs" rel="noreferrer">https://github.com/aspnet/DependencyInjection/blob/master/src/DI/ServiceLookup/ServiceProviderEngineScope.cs</a></p>
  
  <p>Any ideas why <code>ServiceProviderEngineScope</code> had been disposed before MediatR called it?</p>
  
  <p><strong>MediatR</strong> version 7.0.0.</p>
  
  <p><strong>Asp.Net core</strong> 3.0.</p>