title: Is it possible to pass the ServiceProvider into a constructor parameter?
body: <p><strong>Problem:</strong></p>
  <p>We have a .NET 5 WPF application that has an EntityFramework Core entities class file <code>DbEntities</code>, which implements the <code>DbContext</code>. We use constructor injection when instantiating it. One of the options that we use is <code>AddInterceptors</code> in order to append an Access Token to the <code>SqlConnection</code>. The interceptor is called <code>AzureAuthenticationInterceptor</code>. When registering the service, we would like to pass in the <code>ServiceProvider</code> so that it is available in the interceptors constructor, which can be used to get a service that implements Access Token in-memory caching.</p>
  <p>The reason for it is that we have a project with 50+ classes that all use the same <code>DbEntities</code> file, which takes 0 arguments in the constructor. This was upgraded to .NET 5 where Dependency Injection was avoided due to the work it would take to apply it to all of the forms. So, the <code>DbEntities</code> is instantiated in the forms with <code>new DbEntities();</code>.</p>
  <p>But, in this case, we are implementing an access token cache, which needs to be registered as a service. Otherwise, if we just instantiate the cache every time we create a new <code>DbContext</code>, then the cache will be wiped out.</p>
  <p>The access token in-memory cache is implemented using this method <a href="https://mderriey.com/2020/09/12/resolve-ef-core-interceptors-with-dependency-injection/" rel="nofollow noreferrer">https://mderriey.com/2020/09/12/resolve-ef-core-interceptors-with-dependency-injection/</a></p>
  <p>We only want to use dependency injection for the in-memory token cache. The only way we think of as a shortcut is to pass the <code>ServiceProvider</code> in the interceptor's constructor, but it does not appear available in the <code>ConfigureServices</code> method.</p>
  <p><strong>Question:</strong></p>
  <p>Is it possible to pass in the <code>ServiceProvider</code>? If not, is there any other way we can implement dependency injection on the interceptor without having to change 50 class files?</p>
  <p><strong>Program.cs</strong></p>
  <pre><code>Public static void Main()
  {
  ...
      Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
          .ConfigureAppConfiguration((context, builder) =&gt;
          {
              builder.AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true);
          })
          .ConfigureServices((context, services) =&gt;
          {
              Configuration = context.Configuration;
              ConfigureServices(Configuration, services);
          })
          .Build();
  ...
  }
  
  private static void ConfigureServices(IConfiguration objConfiguration, IServiceCollection objServices)
  {
      objServices.AddMemoryCache()
          .AddSingleton&lt;IAzureSqlTokenProvider, AzureIdentityAzureSqlTokenProvider&gt;()
          .Decorate&lt;IAzureSqlTokenProvider, CacheAzureSqlTokenProvider&gt;()
          .AddSingleton(new AzureAuthenticationInterceptor(IServiceProvider_NeededHere))
          ;
  }
  </code></pre>
  <p><strong>DbEntities.cs</strong></p>
  <pre><code>public DbEntities() :
  base(new DbContextOptionsBuilder&lt;DbEntities&gt;()
      .UseSqlServer(ConfigurationManager.ConnectionStrings[&quot;DbEntities&quot;].ConnectionString)
      .AddInterceptors(new AzureAuthenticationInterceptor())
      .Options)
  { }
  </code></pre>
  <p><strong>AzureAuthenticationInterceptor.cs</strong></p>
  <pre><code>public AzureAuthenticationInterceptor(IServiceProvider objServiceProvider)
  {
      this.IAzureSqlTokenProvider = (IAzureSqlTokenProvider)objServiceProvider.GetService(typeof(IAzureSqlTokenProvider));
  }   
  </code></pre>