title: Cannot resolve scoped service dbcontext from root provider
body: <p>I have created an API Logging middleware to log request to DB and to Azure Microsoft insight however for some reason the Dependency Injection for the Database context is not found (gives cannot resolve scoped service from root provider).</p>
  
  <p>API Logging MiddleWare</p>
  
  <pre><code>public class APILoggingMiddleware
  {
      private readonly TelemetryClient _telemetry;
  
      private readonly ILogger _logger;
  
      private readonly RequestDelegate _next;
  
      private APIsAuditContext db;
  
      public static IConfiguration _configuration;
      public APILoggingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, TelemetryClient telemetry, IConfiguration configuration, APIsAuditContext dbContext)
      {
          _configuration = configuration;
  
          _telemetry = telemetry;
  
          _next = next;
  
          _logger = loggerFactory.CreateLogger&lt;APILoggingMiddleware&gt;();
  
          db = dbContext;
      }
  
      //static class to simplify adding the middleware to the application’s pipeline
  
      public async Task Invoke(HttpContext context)
      {
          try
          {
              await _next(context);
          }
          finally
          {
  
              Task logReq = logRequest(context);
              Task lodRes = logResponse(context);
          }
       }
    }
  </code></pre>
  
  <p>DB Context</p>
  
  <pre><code>public class APIsAuditContext : DbContext
  {
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          if (!optionsBuilder.IsConfigured)
          {
          }
      }
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
      }
      public APIsAuditContext(DbContextOptions&lt;APIsAuditContext&gt; options) : base(options) { }
      public DbSet&lt;APIsRequestsAudit&gt; APIsRequestsAudits { get; set; }
  
  
  }
  </code></pre>
  
  <p>Startup</p>
  
  <pre><code> public class Startup
  {
      public IConfiguration _configuration { get; }
  
      public Startup(IConfiguration configuration)
      {
          _configuration = configuration;
      }
  
      public void ConfigureServices(IServiceCollection services)
      {
  
          services.AddDbContext&lt;APIsAuditContext&gt;(options =&gt; options.UseSqlServer(_configuration["ConnectionStrings:APIAuditConnection"]));
  
          services.AddApplicationInsightsTelemetry();
          services.AddControllers();
          services.AddMvc();
      }
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
  
          //Pipeline to API Logging 
          app.UseRequestResponseLogging();
  
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseHttpsRedirection();
          app.UseRouting();
          app.UseAuthorization();
  
          app.UseEndpoints(endpoints =&gt; 
          {
              endpoints.MapControllers();
          });
  
  
  
      }
  }
  </code></pre>
  
  <p>Can someone help please?</p>