Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在dotnetcore中实现开放泛型构造函数注入_C#_.net Core_Dependency Injection_Open Generics - Fatal编程技术网

C# 如何在dotnetcore中实现开放泛型构造函数注入

C# 如何在dotnetcore中实现开放泛型构造函数注入,c#,.net-core,dependency-injection,open-generics,C#,.net Core,Dependency Injection,Open Generics,我有一个审计日志的界面: public interface IAuditDbContext<T1, T2> { T1 AuditLog { get; set; } T2 ChangeTracker { get; } } 公共接口IAuditDbContext { T1审核日志{get;set;} T2 ChangeTracker{get;} } 现在在主AppDbContext接口中,我将其继承为: public interface IAppDBContext<

我有一个审计日志的界面:

public interface IAuditDbContext<T1, T2>
{
   T1 AuditLog { get; set; }
   T2 ChangeTracker { get; }
}
公共接口IAuditDbContext
{
T1审核日志{get;set;}
T2 ChangeTracker{get;}
}
现在在主AppDbContext接口中,我将其继承为:

public interface IAppDBContext<T1, T2> : IAuditDbContext<T1,T2>
{
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
公共接口IAppDBContext:IAuditDbContext
{
任务保存更改同步(CancellationToken CancellationToken);
}
在主AppDbContext类中,我继承为:

public class AppDBContext : DbContext, IAppDBContext<DbSet<Audit>, ChangeTracker>
{
 ....
}
公共类AppDBContext:DbContext,IAppDBContext
{
....
}
问题是,在依赖项注入时,我正试图像这样注入它:

services.AddScoped(
    typeof(IAppDBContext<,>),
    typeof(IAuditDbContext<,>).MakeGenericType(typeof(AppDBContext)));
services.AddScoped(
类型(IAppDBContext),
typeof(IAuditDbContext).MakeGenericType(typeof(AppDBContext));
我也尝试过其他方法,但没有成功- 我收到的一些错误消息包括:

错误消息:

  • Message=提供的泛型参数数量不等于泛型类型定义的arity
  • 无法实例化实现类型“Common.Application.Interfaces”。IAuditDbContext
    2[T1,T2]'用于服务类型“Common.Application.Interfaces.IAppDBContext
    2[T1,T2]”
  • 请求帮助以正确实施它

    typeof(IAuditDbContext).MakeGenericType(typeof(AppDBContext))

    IAuditDbContext
    界面中有两个通用参数,因此需要为
    MakeGenericType
    方法提供两个参数

    但是,我不确定这是否是您所需要的。 您应该将接口映射到可能启动的某些类,而不是接口

    services.AddScoped(typeof(IAppDBContext<,>), typeof(AppDBContext)); //example
    
    services.addScope(typeof(IAppDBContext),typeof(AppDBContext))//例子
    
    但是您真的需要注册开放泛型类型吗