我正在尝试将一个服务注入到另一个服务中,但我无法使它工作,在网上搜索了一下后,我还看不到解决方案。
这是确切的错误:
System.AggregateException:“无法构造某些服务(验证服务描述符”ServiceType:MyBank.services.ITRansactionService Lifetime:作用域实现类型:MyBank.services.TransactionService“时出错:尝试激活”MyBank.services.TransactionService“时无法解析类型”MyBank.services.AccountsService“的服务。)”
我有这样的服务:
public class AccountsService : IAccountsService
我想把它注入到另一个服务中:
public class TransactionService : ITransactionService
{
private readonly ApplicationDbContext db;
private readonly AccountsService accountsService;
public TransactionService(ApplicationDbContext db, AccountsService accountsService)
{
this.db = db;
this.accountsService = accountsService;
}
}
这是我的Startup.cs服务区域:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
_ = services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddRazorPages().
AddRazorRuntimeCompilation();
services.AddScoped<IAccountsService, AccountsService>();
services.AddScoped<ITransactionService, TransactionService>();
}
您正在尝试将AccountsService
(实现),而不是IAccountsService
(接口)注入TransactionService
。