title: cannot access an already disposed object serviceProvider
body: <p>I have a mediator class like this where I initialize a dictionary with a bunch of services:</p>
  <pre><code>public class Mediator : IMediator
      {
          static readonly ConcurrentDictionary&lt;int, Func&lt;IGameBaseService&gt;&gt; services = new ConcurrentDictionary&lt;int, Func&lt;IGameBaseService&gt;&gt;();
          protected readonly IServiceProvider _serviceProvider;
  
          public Mediator(IServiceProvider serviceProvider)
          {
              _serviceProvider = serviceProvider;
          }
          public async Task&lt;Func&lt;IGameBaseService&gt;&gt; GetGameServiceAsync(int gameId)
          {
              if (services.Count == 0)
                  await AddServices();
              return await Task.FromResult(services[gameId]);
          }
  
          private async Task AddServices()
          {
              await Task.FromResult(services.GetOrAdd((int)Game.Blackjack, () =&gt; new LotteryService(_serviceProvider)));
          }
  </code></pre>
  <p>I call GetStateAsync like this to get the game state:</p>
  <pre><code>public async Task&lt;ResponseBase&gt; GetStateAsync(ApiGetStateInput input)
  {
      var gameService = await _mediator.GetGameServiceAsync(input.Identity.GameId);
      var state = gameService().GetStateAsync(input.Identity.GameTableId);
      response.ResponseObject = state.Dump();
  }
  </code></pre>
  <p>The call goes through the mediator to know which game service to call. it creates an instance of the service by passing a servicePrvidor through the constructor. the Lottery game service looks something like this:</p>
  <pre><code>public class LotteryService : BaseService, IGameBaseService
      {
         public LotteryService (IServiceProvider serviceProvider) : base(serviceProvider)
          {
              
          }
      }
  public Task&lt;IEnumerable&lt;BlGameState&gt;&gt; GetStateAsync(int tableId)
          {
              //logic
              return something;
          }
  </code></pre>
  <pre><code> public class BaseService : IBaseService
      {
          protected readonly IServiceProvider _serviceProvider;
          protected readonly IUnitOfWork _unitOfWork;
          protected readonly ICacheProvider _cache;
          protected readonly IMapper _mapper;
  
          public BaseService(IServiceProvider serviceProvider)
          {
              _serviceProvider = serviceProvider;
              _mapper = _serviceProvider.GetService&lt;IMapper&gt;();
              _unitOfWork = _serviceProvider.GetService&lt;IUnitOfWork&gt;();
              _cache = _serviceProvider.GetService&lt;ICacheProvider&gt;();
          }
  }
  </code></pre>
  <p>And I have registered all this in the ConfigureServices of startup:</p>
  <pre><code>            services.AddAutoMapper(typeof(Startup));
              
              services.AddScoped&lt;IUnitOfWork, UnitOfWork&gt;();
              services.AddScoped&lt;ICacheProvider, CacheProvider&gt;();
              services.AddScoped&lt;IMediator, Mediator&gt;();
  </code></pre>
  <p>The problem is when I run the project and call GetStateAsync, It works perfectly fine. But after calling it again looks like serviceProvider is being disposed and is not being able get a serviceProvider instance. How to solve this issue and is there a way to improve this code and architecture of getting the services?
  Here is the error:</p>
  <pre><code>Cannot access a disposed object.\r\nObject name: 'IServiceProvider'._   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()\r\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)\r\n   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)\r\n
  </code></pre>