title: ASP.NET Core 2.1 unable to cast IServiceProvider
body: <p>My application ASP.net web API project on core 2.1</p>
  
  <p>I have the following IOC container class</p>
  
  <pre><code>// Ioc.cs
  using Microsoft.Extensions.DependencyInjection;
  using Rendigo.Core.Data;
  
  public static class Ioc
  {  
      public static RendigoContext RendigoContext =&gt; 
      IocContainer.Provider.GetService&lt;RendigoContext&gt;();
  }
  
  // IocContainer.cs
  using Microsoft.Extensions.DependencyInjection;
  
  public static class IocContainer
  {
      public static ServiceProvider Provider { get; set; }
  }
  
  //startup.cs 
  
  
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
                      IServiceProvider serviceProvider)
      {
          // exception is raised here 
          IocContainer.Provider = (ServiceProvider)serviceProvider;
           var context = Ioc.RendigoContext;
      // .....
      }
  </code></pre>
  
  <p>The exception detail</p>
  
  <blockquote>
    <p>System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' to type 'Microsoft.Extensions.DependencyInjection.ServiceProvider'.'</p>
  </blockquote>
  
  <ul>
  <li>I used this approach with ASP core 2.0 and it was working fine.</li>
  <li><p>I have 2 questions on this:</p>
  
  <ol>
  <li><p>Why I am getting this exception am I missing some code to place in program.cs
  or a library needs to be imported via nuget.</p></li>
  <li><p>Is this approach of making the context static is a good approach to follow.</p></li>
  </ol></li>
  </ul>