C# 具有通用服务和存储库代码的.NET核心依赖项注入错误
我正在尝试将我在所有项目中使用的.NET framework中的通用代码模板移动到.NET Core,而我在.NET Core中的依赖项注入方面遇到了一些问题 这是我的密码: API控制器:C# 具有通用服务和存储库代码的.NET核心依赖项注入错误,c#,.net,entity-framework,.net-core,C#,.net,Entity Framework,.net Core,我正在尝试将我在所有项目中使用的.NET framework中的通用代码模板移动到.NET Core,而我在.NET Core中的依赖项注入方面遇到了一些问题 这是我的密码: API控制器: [Route("api/[controller]")] [ApiController] public class ProductController : ControllerBase { private readonly IProductService _productServi
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;
public ProductController(IProductService productService)
{
_productService = productService;
}
[HttpGet]
public IActionResult GetAll()
{
var products = _productService.GetAll();
return Ok(products);
}
}
产品服务:
public class ProductService : ServiceBase<Product, BaseRepository<Product>>, IProductService
{
public ProductService(BaseRepository<Product> rep) : base(rep)
{
}
}
public interface IProductService : IServiceBase<Product>
{
}
公共类ProductService:ServiceBase,IPProductService
{
公共产品服务(基本存储库代表):基本(代表)
{
}
}
IPProductService:
public class ProductService : ServiceBase<Product, BaseRepository<Product>>, IProductService
{
public ProductService(BaseRepository<Product> rep) : base(rep)
{
}
}
public interface IProductService : IServiceBase<Product>
{
}
公共接口IPProductService:IServiceBase
{
}
IServiceBase:
public interface IServiceBase<TEntity>
{
IEnumerable<TEntity> GetAll();
}
public abstract class ServiceBase<TEntity, TRepository> : IServiceBase<TEntity>
where TEntity : class
where TRepository : BaseRepository<TEntity>
{
public TRepository Repository;
public ServiceBase(BaseRepository<TEntity> rep)
{
Repository = (TRepository)rep;
}
public IEnumerable<TEntity> GetAll()
{
return Repository.GetAll();
}
}
公共接口IServiceBase
{
IEnumerable GetAll();
}
服务基地:
public interface IServiceBase<TEntity>
{
IEnumerable<TEntity> GetAll();
}
public abstract class ServiceBase<TEntity, TRepository> : IServiceBase<TEntity>
where TEntity : class
where TRepository : BaseRepository<TEntity>
{
public TRepository Repository;
public ServiceBase(BaseRepository<TEntity> rep)
{
Repository = (TRepository)rep;
}
public IEnumerable<TEntity> GetAll()
{
return Repository.GetAll();
}
}
公共抽象类ServiceBase:IServiceBase
地点:班级
存放地点:BaseRepository
{
公共存储库;
公共服务库(BaseRepository)
{
存储库=(存储)代表;
}
公共IEnumerable GetAll()
{
返回Repository.GetAll();
}
}
基本存储库:
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private readonly DatabaseContext _dbContext;
public BaseRepository(DatabaseContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<T> GetAll()
{
return _dbContext.Set<T>();
}
}
public interface IBaseRepository<T> where T : class
{
IEnumerable<T> GetAll();
}
public class BaseRepository:IBaseRepository其中T:class
{
私有只读数据库上下文_dbContext;
公共BaseRepository(DatabaseContext dbContext)
{
_dbContext=dbContext;
}
公共IEnumerable GetAll()
{
返回_dbContext.Set();
}
}
IBaseRepository:
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private readonly DatabaseContext _dbContext;
public BaseRepository(DatabaseContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<T> GetAll()
{
return _dbContext.Set<T>();
}
}
public interface IBaseRepository<T> where T : class
{
IEnumerable<T> GetAll();
}
公共接口IBaseRepository,其中T:class
{
IEnumerable GetAll();
}
数据库上下文:
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
}
公共类数据库上下文:DbContext
{
公共数据库上下文(DbContextOptions):基本(选项)
{
}
公共数据库集产品{get;set;}
}
启动文件:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
x => x.MigrationsAssembly("AdministrationPortal.Data")));
services.AddTransient<IProductService, ProductService>();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),
x=>x.migrationassembly(“AdministrationPortal.Data”);
services.AddTransient();
services.AddControllers();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
});
}
}
尝试使用F5(或绿色播放按钮)启动API项目时出现的错误:
System.AggregateException:'无法构造某些服务(验证服务描述符“ServiceType:AdministrationPortal.Interfaces.Services.IPProductService生存期:临时实现类型:AdministrationPortal.Core.Services.ProductService”时出错:无法解析“AdministrationPortal.Repositories.Repositories.BaseRepository”类型“1”的服务[AdministrationPortal.Domain.Entities.Product]'试图激活“AdministrationPortal.Core.Services.ProductService”。)
非常感谢您的帮助!您的
ProductService
类正在尝试解决对BaseRepository
的依赖关系,但您尚未在服务提供商中注册该依赖关系。在启动
类中,您只需注册该类型即可:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
x => x.MigrationsAssembly("AdministrationPortal.Data")));
services.AddTransient<IProductService, ProductService>();
services.AddTransient<BaseRepository<Product>>(); // <----- This is needed
services.AddControllers();
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),
x=>x.migrationassembly(“AdministrationPortal.Data”);
services.AddTransient();
services.AddTransient();//services.AddTransient()
:您显示的类/接口不使用IPProductRepository
/ProductRepository
。是否应该使用它们?如果不是,它们是用于什么?@devNull-Edited Startup.cs文件,它是冗余代码。很抱歉,这听起来像是“Transient”的一个或多个依赖项不可用…请确保您已正确安装其软件包。请尽力而为。在您编辑您的评论之前,我尝试了services.AddTransient();
,并且效果良好。然后,我想知道如何使其通用化,同时您用精确的解决方案编辑了您的评论!:)