Unable to resolve service for type 'IBatchProducts' while attempting to activate 'ProductBatcherController'

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月17日 16:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定