ASP.NET MVC项目添加Identity后运行出现依赖注入错误
ASP.NET MVC添加Identity组件后依赖注入报错解决
错误信息:构造服务描述符时出错(验证服务描述符 'ServiceType: CandySoap.DataAccess.Repository.IRepository.IUnitOfWork Lifetime: Scoped ImplementationType: CandySoap.DataAccess.Repository.UnitOfWork' 失败:无法解析依赖项)
问题原因
依赖注入容器无法解析UnitOfWork初始化时用到的仓储接口(ICategoryRepository、ICovertypesRepository、IProductRepository),因为这些接口未注册到服务容器中。
解决方案
在Program.cs的服务注册环节,新增所有仓储接口与实现类的依赖注入配置:
// 注册仓储接口及其实现 builder.Services.AddScoped<ICategoryRepository, CategoryRepository>(); builder.Services.AddScoped<ICovertypesRepository, CovertypesRepository>(); builder.Services.AddScoped<IProductRepository, ProductRepository>();
同时检查以下两点:
- 确保所有仓储类已正确实现对应接口
ApplicationContext的继承与连接字符串配置无问题
相关代码参考
Program.cs 代码
using CandySoap.Data; using CandySoap.DataAccess.Repository; using CandySoap.DataAccess.Repository.IRepository; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; var builder = WebApplication.CreateBuilder(args); // 向容器添加服务 builder.Services.AddControllersWithViews(); builder.Services.AddDbContext<ApplicationContext>(option => option.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionLink"))); builder.Services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<ApplicationContext>(); // 新增:注册所有仓储接口 builder.Services.AddScoped<ICategoryRepository, CategoryRepository>(); builder.Services.AddScoped<ICovertypesRepository, CovertypesRepository>(); builder.Services.AddScoped<IProductRepository, ProductRepository>(); builder.Services.AddScoped<IUnitOfWork, UnitOfWork>(); var app = builder.Build(); // 配置HTTP请求管道 if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // 默认HSTS值为30天,生产环境可根据需要修改 app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}"); app.Run(); // 注:此处using语句位置错误,应移至文件顶部 // using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
ApplicationContext 类代码
using Microsoft.EntityFrameworkCore; namespace CandySoap.Data { public class ApplicationContext : IdentityDbContext { public ApplicationContext(DbContextOptions<ApplicationContext> option) : base(option) { } public DbSet<Category> categories { get; set; } public DbSet<Covertype> covertypes { get; set; } public DbSet<Product> products { get; set; } } }
UnitOfWork 类代码
using CandySoap.Data; using CandySoap.DataAccess.Repository.IRepository; namespace CandySoap.DataAccess.Repository { public class UnitOfWork : IUnitOfWork { private ApplicationContext _db; public UnitOfWork(ApplicationContext db) { _db = db; Category = new CategoryRepository(_db); Covers = new CovertypesRepository(_db); Product = new ProductRepository(_db); } public ICategoryRepository Category { get; private set; } public ICovertypesRepository Covers { get; private set; } public IProductRepository Product { get; private set; } public void Save() { _db.SaveChanges(); } } }
内容的提问来源于stack exchange,提问作者baba
相关产品推荐
相关产品推荐

