所以,當我開始在 DI 容器中包含我的依賴項時出現問題。我有一個資料庫的資料訪問庫。當我嘗試在 DI 容器中添加它時,它在 app.build 陳述句中失敗,有 3 個與相同錯誤相關的例外。例外:
!System.AggregateException:無法構造某些服務(驗證服務描述符'ServiceType:DAL.Repos.Interfaces.ICarRepo Lifetime:Scoped ImplementationType:DAL.Repos.CarRepo'時出錯:無法激活型別'DAL.Repos。 CarRepo'。以下建構式不明確: Void .ctor(DAL.EfMainStructures.ApplicationContext) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[DAL.EfMainStructures.ApplicationContext]))
!System.InvalidOperationException:驗證服務描述符“ServiceType:DAL.Repos.Interfaces.ICarRepo Lifetime:Scoped ImplementationType:DAL.Repos.CarRepo”時出錯:無法激活型別“DAL.Repos.CarRepo”。以下建構式不明確: Void .ctor(DAL.EfMainStructures.ApplicationContext) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[DAL.EfMainStructures.ApplicationContext])
!System.InvalidOperationException:無法激活型別“DAL.Repos.CarRepo”。以下建構式不明確: Void .ctor(DAL.EfMainStructures.ApplicationContext) Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[DAL.EfMainStructures.ApplicationContext])
所有這些都與建構式相關,但我無法理解它的含義。這是我的 Program.cs 代碼
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddDbContextPool<ApplicationContext>(options =>
options.UseNpgsql(connectionString,sqlOp =>
sqlOp.EnableRetryOnFailure()));
builder.Services.AddScoped<ICarRepo,CarRepo>();
/*builder.Services.AddScoped<ICustomerRepo,CustomerRepo>();
builder.Services.AddScoped<ICreditRiskRepo,CreditRiskRepo>();
builder.Services.AddScoped<IMakeRepo,MakeRepo>();
builder.Services.AddScoped<IOrderRepo,OrderRepo>();*/
/*builder.Services.AddScoped(typeof(IAppLogging<>),typeof(AppLogging<>));
builder.Host.ConfigureSerilog();*/
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
if (app.Configuration.GetValue<bool>("RebuildDataBase"))
{
var context = new DatabaseContextFactory().CreateDbContext(new string[1]);
SampleDataInitializer.InitializeData(context);
}
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
這是我的 ApplicationContext 代碼
public partial class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options)
: base(options)
{
}
public virtual DbSet<CreditRisk>? CreditRisks { get; set; }
public virtual DbSet<Customer>? Customers { get; set; }
public virtual DbSet<Make>? Makes { get; set; }
public virtual DbSet<Car>? Cars { get; set; }
public virtual DbSet<Order>? Orders { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
這個 GitHub 存盤庫:https ://github.com/DIDIVERG/Train (有考慮 MVC 專案)
它必須將我的介面及其實作添加到 DI 容器,但它不會并拋出上述例外。如果這個問題是陳詞濫調,我很抱歉。
uj5u.com熱心網友回復:
根據提供的錯誤訊息DAL.Repos.CarRepo,有兩個建構式和一個引數,因此 DI 無法確定使用哪一個。洗掉其中一個建構式。
uj5u.com熱心網友回復:
重要的一段代碼是您的 CarRepo,它位于 Git Repo 中未包含的依賴項中。
看起來它有兩個公共建構式,并且 DI 容器能夠為兩者提供引數,因此它無法決定應該使用哪個建構式并報告“模糊”。
我的猜測是您的 CarRepo 具有以下內容:
public CarRepo(ApplicationContext context)
{
}
public CarRepo(DbContextOptions<ApplicationContext> options)
{
}
在回購中,您應該只需要第一個。DbContextOptions 在 DI 容器實體化 ApplicationContext 時使用。
uj5u.com熱心網友回復:
正如其他人提到的,DI 容器無法決定使用哪個建構式。解決它的一種選擇是通過提供工廠方法明確告訴它使用哪一個:
builder.Services.AddScoped<ICarRepo>(
sp => new CarRepo(sp.GetRequiredService<YourClassDependencyHere>()));
我不喜歡這種方法,因為每次建構式簽名更改時都需要更新,但這是一個簡單的修復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519458.html
標籤:C#asp.net-mvcasp.net 核心实体框架核心
