英文:
Unable to resolve service for type 'IBatchProducts' while attempting to activate 'ProductBatcherController'
问题
Here's the translation of the provided text:
"UPDATE: I created a GitHub repository with a load of stuff taken out (not sure if fully minimal) which still produces the same error message: https://github.com/georalroscoe/DebuggingServiceIssue.git
This is the error message:
> System.InvalidOperationException: Unable to resolve service for type 'Application.Interfaces.IBatchProducts' while attempting to activate 'PrivateWebAPI.Controllers.ProductBatcherController'.
Repository
public interface IBatchProducts
{
}
public class ProductBatcher : IBatchProducts
{
private readonly IUnitOfWork _uow;
private readonly IGenericRepository<WarehouseBatchContent> _warehouseBatchContentRepo;
public ProductBatcher(
IGenericRepository<WarehouseBatchContent> warehouseBatchContentRepo,
IUnitOfWork uow)
{
_warehouseBatchContentRepo = warehouseBatchContentRepo;
_uow = uow;
}
}
Controller
public class ProductBatcherController : ControllerBase
{
private readonly IBatchProducts _productBatcher;
public ProductBatcherController(IBatchProducts productBatcher)
{
_productBatcher = productBatcher;
}
// POST api/<ProductBatcherController>
[HttpPost]
public void Post([FromBody] ProductBatcherDto dto)
{
_productBatcher.BatchMover(dto);
}
}
Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<DepotContext>(options => options
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<DbContext, DepotContext>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddTransient<IBatchProducts, ProductBatcher>();
services.AddTransient<IGenericRepository<WarehouseBatchContent>, GenericRepository<WarehouseBatchContent>>();
services.AddTransient<IGenericRepository<WarehouseBatch>, GenericRepository<WarehouseBatch>>();
services.AddControllers();
services.AddSwaggerGen();
}
I've provided the translation as requested.
英文:
UPDATE: I created a github repository with a load of stuff taken out(not sure if fully minimal) which still produces the same error message: https://github.com/georalroscoe/DebuggingServiceIssue.git
This is the error message:
> System.InvalidOperationException: Unable to resolve service for type 'Application.Interfaces.IBatchProducts' while attempting to activate 'PrivateWebAPI.Controllers.ProductBatcherController'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
Repository
public interface IBatchProducts
{
}
public class ProductBatcher : IBatchProducts
{
private readonly IUnitOfWork _uow;
private readonly IGenericRepository<WarehouseBatchContent> _warehouseBatchContentRepo;
public ProductBatcher(
IGenericRepository<WarehouseBatchContent> warehouseBatchContentRepo,
IUnitOfWork uow)
{
_warehouseBatchContentRepo = warehouseBatchContentRepo;
_uow = uow;
}
}
Controller
public class ProductBatcherController : ControllerBase
{
private readonly IBatchProducts _productBatcher;
public ProductBatcherController(IBatchProducts productBatcher)
{
_productBatcher = productBatcher;
}
// POST api/<ProductBatcherController>
[HttpPost]
public void Post([FromBody] ProductBatcherDto dto)
{
_productBatcher.BatchMover(dto);
}
}
Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<DepotContext>(options => options
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<DbContext, DepotContext>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddTransient<IBatchProducts, ProductBatcher>();
services.AddTransient<IGenericRepository<WarehouseBatchContent>, GenericRepository<WarehouseBatchContent>>();
services.AddTransient<IGenericRepository<WarehouseBatch>, GenericRepository<WarehouseBatch>>();
services.AddControllers();
services.AddSwaggerGen();
}
I've looked over it countless times and can't see what I'm doing wrong. Any help?
I expected this to send a post request using swagger to api/<ProductBatcherController>.ü
答案1
得分: 1
问题在于我在不需要时使用了一个启动文件,配置应该在我的程序文件中。
英文:
The problem was I was using a startup file when it wasn't needed, the configuration should have been in my program file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论