Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
When I attempt requests to a .net core 3.1 WebAPI from Postman I am getting error '
System.InvalidOperationException: Unable to resolve service for type 'PaymentsAPI.Repository.PaymentService' while attempting to activate 'PaymentsAPI.Controllers.PaymentController'
'

Startup.cs

C#
<pre>public void ConfigureServices(IServiceCollection services)
        {      
            services.AddControllers();
          
            services.AddCors(c =>
            {
                c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
            });
            services.AddDbContext<ApplicationDbContext>(o => o.UseSqlServer(Configuration.GetConnectionString("SqlSvrConn")));
            services.AddTransient<IAsyncPaymentsService<PaymentDetail>, PaymentService>();
}


IAsyncPaymentsService.cs
C#
<pre> public interface IAsyncPaymentsService<TEntity>

    {        
        Task<IEnumerable<TEntity>> GetAllAsync();
    }



PaymentService.cs
C#
<pre>public class PaymentService : IAsyncPaymentsService<PaymentDetail>
    {
        private readonly ApplicationDbContext _dbContext;
        

        public async Task<IEnumerable<PaymentDetail>> GetAllAsync()
        {
            return await _dbContext.PaymentDetails.ToListAsync();
        }
    }



PaymentController.cs
C#
<pre> [ApiController]
    [Route("[controller]")]
    public class PaymentController : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        private readonly PaymentService _service;
        public PaymentController(ApplicationDbContext context, PaymentService service)
        {
            _context = context;
            _service = service;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<PaymentDetail>>> GetAsync()
        {
            var items = (await _service.GetAllAsync());
            return Ok(items);
        }

}



What am I missing?

What I have tried:

I have tried rearranging the order of services in the container but the error still persists.
Posted
Updated 29-Apr-22 7:14am

You have registered PaymentService as an implementation of the IAsyncPaymentsService<PaymentDetail> service.

Your controller needs to import the IAsyncPaymentsService<PaymentDetail> service, not the class that implements it:
C#
private readonly IAsyncPaymentsService<PaymentDetail> _service;

public PaymentController(ApplicationDbContext context, IAsyncPaymentsService<PaymentDetail> service)
{
    ...
 
Share this answer
 
Did you try to register the type in the ConfigurationServices?

C#
<pre>public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDBContext>(options => options.UseSqlServer(Configuration["ConnectionString:MyDBConnectionString"]));
    services.AddScoped<IDataRepository<Lead>, LeadManager>();
    services.AddControllers();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900