AggregateException:某些服务无法构建
AggregateException: Some services are not able to be constructed
我在 .NET 5.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'.)
我简而言之 - 每个存储库、每个服务、所有需要注册并已在 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");
});
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
不知道是不是Rider版本的问题(我在macOS上编程)。调试时出现以下错误消息:
An IL variable is not available at the current native IP. (0x80131304). The error code is CORDBG_E_IL_VAR_NOT_AVAILABLE, or 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 容器使用构造函数来发挥它的魔力,并尝试解析和注入所有 constructor parameters。由于您还没有(也不应该)注册 DbSet
- 它们无法解析。
我在 .NET 5.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'.)
我简而言之 - 每个存储库、每个服务、所有需要注册并已在 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");
});
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
不知道是不是Rider版本的问题(我在macOS上编程)。调试时出现以下错误消息:
An IL variable is not available at the current native IP. (0x80131304). The error code is CORDBG_E_IL_VAR_NOT_AVAILABLE, or 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 容器使用构造函数来发挥它的魔力,并尝试解析和注入所有 constructor parameters。由于您还没有(也不应该)注册 DbSet
- 它们无法解析。