0

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).

API Logging MiddleWare

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<APILoggingMiddleware>();

        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);
        }
     }
  }

DB Context

public class APIsAuditContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
        }
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
    public APIsAuditContext(DbContextOptions<APIsAuditContext> options) : base(options) { }
    public DbSet<APIsRequestsAudit> APIsRequestsAudits { get; set; }


}

Startup

 public class Startup
{
    public IConfiguration _configuration { get; }

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<APIsAuditContext>(options => 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 => 
        {
            endpoints.MapControllers();
        });



    }
}

Can someone help please?

Brian
  • 197
  • 4
  • 14
  • Does this answer your question? [Cannot resolve scoped service from root provider .Net Core 2](https://stackoverflow.com/questions/48590579/cannot-resolve-scoped-service-from-root-provider-net-core-2) – kasptom Apr 19 '20 at 10:15

1 Answers1

1

As per https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-3.1#per-request-middleware-dependencies, you must inject scoped service in the Invoke method:

public class CustomMiddleware
{
    private readonly RequestDelegate _next;

    public CustomMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    // IMyScopedService is injected into Invoke
    public async Task Invoke(HttpContext httpContext, IMyScopedService svc)
    {
        svc.MyProperty = 1000;
        await _next(httpContext);
    }
}
Deepak Mishra
  • 2,984
  • 1
  • 26
  • 32
  • @Brian (maybe pointing out the obvious), in your case `IMyScopedService` is `APIsAuditContext dbContext` or, better option, `IAPIsAuditContext` (you can inject it directly, without the interface but it is a bad practice). – kasptom Apr 19 '20 at 10:24