how to implement Open Generic Constructor Injection in Dot Net Core

I have an interface for Audit Logging:

public interface IAuditDbContext<T1, T2>
{
   T1 AuditLog { get; set; }
   T2 ChangeTracker { get; }
}

now in main AppDbContext interface, I am inheriting this as:

public interface IAppDBContext<T1, T2> : IAuditDbContext<T1,T2>
{
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}

and in main AppDbContext class, I am inheriting as:

public class AppDBContext : DbContext, IAppDBContext<DbSet<Audit>, ChangeTracker>
{
 ....
}

The problem is, at the time of Dependency Injection, I am trying to inject it like:

services.AddScoped(typeof(IAppDBContext<,>), typeof(IAuditDbContext<,>).MakeGenericType(typeof(AppDBContext)));

I have tried other ways as well, but not able to get the success - some error messages that I am getting are:

Error Messages:

  1. Message=The number of generic arguments provided doesn't equal the arity of the generic type definition.
  2. Cannot instantiate implementation type 'Common.Application.Interfaces. IAuditDbContext2[T1,T2]' for service type 'Common.Application.Interfaces.IAppDBContext2[T1,T2]'

Requesting help to implement it correctly.



Read more here: https://stackoverflow.com/questions/64394474/how-to-implement-open-generic-constructor-injection-in-dot-net-core

Content Attribution

This content was originally published by anurag singh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: