无法注入泛型依赖项C#
我正在尝试创建一个在通用存储库中使用的基本DbContext 目标是在微服务环境中使用一个集中的基本存储库和服务,但每个服务必须传递用于查询的已确定dbContext的类型 已尝试将TContext类型从ApiDbContext转换为纯DbContext,但未成功 API在.NET Core 2.2和EFCore 2.2.6上运行 代码如下 存储库实现无法注入泛型依赖项C#,c#,dependency-injection,.net-core,C#,Dependency Injection,.net Core,我正在尝试创建一个在通用存储库中使用的基本DbContext 目标是在微服务环境中使用一个集中的基本存储库和服务,但每个服务必须传递用于查询的已确定dbContext的类型 已尝试将TContext类型从ApiDbContext转换为纯DbContext,但未成功 API在.NET Core 2.2和EFCore 2.2.6上运行 代码如下 存储库实现 public class Repository<TContext> : IDisposable, IRepository<TC
public class Repository<TContext> : IDisposable, IRepository<TContext>
where TContext : ApiDbContext, new()
公共类存储库:IDisposable,IRepository
其中TContext:ApiDbContext,new()
存储库接口
public interface IRepository<TContext> : IDisposable where TContext : ApiDbContext
公共接口IRepository:IDisposable,其中TContext:ApiDbContext
ApidContext
public class ApiDbContext : DbContext
{
private static readonly bool[] _migrated = { false };
private readonly IHttpContextAccessor _httpContextAccessor;
public ApiDbContext()
{
}
public ApiDbContext(DbContextOptions<ApiDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(options)
{
_httpContextAccessor = httpContextAccessor;
Database.SetCommandTimeout(300);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new AuditConfiguration(modelBuilder.Entity<Audit>());
foreach (IMutableForeignKey relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
protected static DbContextOptions<T> ChangeOptionsType<T>(DbContextOptions options) where T : DbContext
{
var sqlExt = options.Extensions.FirstOrDefault(e => e is SqlServerOptionsExtension);
if (sqlExt == null)
throw (new Exception("Failed to retrieve SQL connection string for base Context"));
return new DbContextOptionsBuilder<T>()
.UseSqlServer(((SqlServerOptionsExtension)sqlExt).ConnectionString)
.Options;
}
}
公共类ApiDbContext:DbContext
{
私有静态只读bool[]_migrated={false};
专用只读IHttpContextAccessor\u httpContextAccessor;
公共ApiDbContext()
{
}
公共ApiDbContext(DbContextOptions选项,IHttpContextAccessor httpContextAccessor)
:基本(选项)
{
_httpContextAccessor=httpContextAccessor;
SetCommandTimeout(300);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
新的AuditConfiguration(modelBuilder.Entity());
foreach(modelBuilder.Model.GetEntityTypes()中的IMutableForeignKey关系。SelectMany(e=>e.GetForeignKey())
{
relationship.DeleteBehavior=DeleteBehavior.Restrict;
}
基于模型创建(modelBuilder);
}
受保护的静态DbContextOptions更改选项类型(DbContextOptions选项),其中T:DbContext
{
var sqlExt=options.Extensions.FirstOrDefault(e=>e是SqlServerOptionsExtension);
if(sqlExt==null)
抛出(新异常(“检索基本上下文的SQL连接字符串失败”);
返回新的DbContextOptionsBuilder()
.UseSqlServer(((SqlServerOptionsExtension)sqlExt).ConnectionString)
.选择;
}
}
AuthDbContext
public class AuthDbContext : ApiDbContext
{
public AuthDbContext(DbContextOptions<AuthDbContext> options, IHttpContextAccessor httpContextAccessor)
: base(ChangeOptionsType<ApiDbContext>(options), httpContextAccessor)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
new UserConfiguration(modelBuilder.Entity<User>());
base.OnModelCreating(modelBuilder);
}
}
公共类AuthDbContext:ApiDbContext
{
公共AuthDbContext(DbContextOptions选项,IHttpContextAccessor httpContextAccessor)
:base(更改选项类型(选项),httpContextAccessor)
{
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
新的用户配置(modelBuilder.Entity());
基于模型创建(modelBuilder);
}
}
例外情况
TypeLoadException:Company.Project.Common.\u Bases.Infrastructure.Repository`1[TContext]'上的GenericArguments[0]、'Company.Project.Auth.Infrastructure.Data.AuthDbContext'违反了类型参数'TContext'的约束
引发异常的代码(在存储库注入时)
公共类服务:iSeries
其中T:BaseEntity
其中TContext:ApiDbContext
{
受保护的IRepository _存储库{get;set;}
公共服务(IServiceProvider提供商)
{
_repository=provider.GetService(typeof(IRepository))作为IRepository;
}
}
依赖性注射
public static void Resolve<T>(IServiceCollection services, IConfiguration Configuration) where T : DbContext
{
services.AddDbContext<T>(options =>
options.UseSqlServer(Configuration.GetConnectionString(Configuration["StartupConfig:DefaultConnectionString"]),
b => b.MigrationsAssembly(Configuration["StartupConfig:MigrationAssembly"])
.UseRowNumberForPaging()));
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient(typeof(IService<>), typeof(Service<,>));
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
}
publicstaticvoidresolve(IServiceCollection服务,IConfiguration配置),其中T:DbContext
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(配置[“StartupConfig:DefaultConnectionString]”),
b=>b.MigrationAssembly(配置[“StartupConfig:MigrationAssembly”])
.UseRowNumberForPaging());
services.TryAddSingleton();
服务.AddTransient(类型化(iSeries设备),类型化(服务));
AddTransient(typeof(IRepository)、typeof(Repository));
}
尝试添加
.AddTransient<DbContext, ApiDbContext>();
.AddTransient();
或
.AddTransient();
什么代码会引发该异常?@Jasen编辑。异常被抛出到存储库InjectionThreaded,相同的异常
.AddTransient<DbContext, ApiDbContext>();
.AddTransient<ApiDbContext>();