C# AggregateException:无法构造某些服务

C# AggregateException:无法构造某些服务,c#,.net,asp.net-core,dependency-injection,.net-5,C#,.net,Asp.net Core,Dependency Injection,.net 5,我在.NET5.0中有一个项目,在4月10日之前,一切都很好——构建、执行、工作 现在我在注册服务时遇到了很多错误 Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AService.DataAccess.MyDbContext Life

我在.NET5.0中有一个项目,在4月10日之前,一切都很好——构建、执行、工作

现在我在注册服务时遇到了很多错误

Unhandled exception. System.AggregateException: Some services are not able to be constructed 
(Error while validating the service descriptor 'ServiceType: AService.DataAccess.MyDbContext 
Lifetime: Scoped ImplementationType: AService.DataAccess.MyDbContext': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbSet`1[AService.Domain.Entities.B.B]' while 
attempting to activate 'AService.DataAccess.MyDbContext'.) (Error while validating the 
service descriptor 'ServiceType: 
MediatR.IRequestHandler`2[AService.Api.Queries.B.GetAllQuery, 
System.Collections.Generic.IEnumerable`1[AService.Api.Queries.B.Dtos.BDto]] 
Lifetime: Transient ImplementationType: 
AService.Queries.B.GetAllQueryHandler': Unable to resolve service for type 
'Microsoft.EntityFrameworkCore.DbSet`1[AService.Domain.Entities.B.B]' while 
attempting to activate 'AService.DataAccess.MyDbContext'.)
I short-每个存储库、每个服务、所有需要注册并在Startup.cs中注册的内容,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson(options => 
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);

    services.AddDbContext<MyDbContext>();
    
    services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>((options) => 
        Configuration.GetSection("Redis").Get<RedisConfiguration>());
    
    services.AddMediatR(Assembly.GetExecutingAssembly());

    services.AddTransient<IARepository, ARepository>();
    services.AddTransient<IBRepository, BRepository>();
    services.AddTransient<ICRepository, CRepository>();

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo {Title = "Service API", Version = "v1.0"});
    });

}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseStaticFiles();
    app.UseRouting();
    app.UseCors();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Menu Service API v1.0");
    });
}
我不知道macos上的Rider版本I程序是否有问题。调试时,出现以下错误消息:

IL变量在当前本机IP上不可用。0x80131304。错误代码为CORDBG_E_IL_VAR_NOT_AVAILABLE,或0x80131304

有人知道是什么导致了这个错误吗?正如我在开始时所说,前几天一切正常

编辑-添加了MyDbContext:

public class MyDbContext : DbContext
{
    public MyDbContext(DbSet<A> a, DbSet<B> b, DbSet<C> c)
    {
        As = a;
        Bs = b;
        Cs = c;
    }

    public DbSet<A> As { get; }
    public DbSet<B> Bs { get; }
    public DbSet<C> Cs { get; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=localhost;Database=my-db;User=user;Password=password");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<B>()
            .HasOne(mi => mi.A)
            .WithMany(m => m.Bs)
            .HasForeignKey(mi => mi.AId);

        modelBuilder.Entity<B>()
            .Property(mi => mi.Xs)
            .HasConversion(
                a => string.Join(',', a),
                a => a.Split(',', StringSplitOptions.RemoveEmptyEntries));
    }
}

删除构造函数并将setter添加到DbSet属性:

public class MyDbContext : DbContext
{
    public DbSet<A> As { get; private set; } // or public setters
    public DbSet<B> Bs { get; private set; }
    public DbSet<C> Cs { get; private set; }
    .....
}

DI容器使用构造函数来实现它的魔力,并尝试解析和注入所有。由于您没有并且不应该注册DbSet,因此无法解析它们。

删除构造函数并将setter添加到DbSet属性:

public class MyDbContext : DbContext
{
    public DbSet<A> As { get; private set; } // or public setters
    public DbSet<B> Bs { get; private set; }
    public DbSet<C> Cs { get; private set; }
    .....
}

DI容器使用构造函数来实现它的魔力,并尝试解析和注入所有。因为你没有也不应该注册数据库集-它们无法解析。

你能为MyDbContext添加代码吗?我已经编辑了主要帖子。你应该从数据库上下文中删除构造函数。天哪。。。代码的附加提示非常棒。。。我花了两天的时间弄清楚这是怎么回事。您知道为什么会出现这样的问题吗?错误消息清楚地表明:您的服务定位器无法构造DbContext。原因是服务定位器无法注入DbSet a、DbSet b和DbSet c,除非您在Startup.cs中的ConfigureService方法中注册它们。或者,您可以通过AddDbContext参数提供一个方法来覆盖dbcontext的创建代码。您可以为MyDbContext添加代码吗?我已经编辑了主要帖子。您应该从db上下文中删除构造函数。天哪。。。代码的附加提示非常棒。。。我花了两天的时间弄清楚这是怎么回事。您知道为什么会出现这样的问题吗?错误消息清楚地表明:您的服务定位器无法构造DbContext。原因是服务定位器无法注入DbSet a、DbSet b和DbSet c,除非您在Startup.cs中的ConfigureService方法中注册它们。或者,您也可以通过AddDbContext参数提供一个方法来覆盖dbcontext的创建代码。