Home > Mobile >  Getting Error : Some services are not able to be constructed in .NET CORE API
Getting Error : Some services are not able to be constructed in .NET CORE API

Time:10-18

I have created simple .net core web api using visual studio 2022. To that application I have added a services for getting Employees data to the Controller. But when I run my application I am getting following error.

Error Message :

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: EmployeeAPI.Services.EmployeeService.IEmployeeRepository Lifetime: Scoped ImplementationType: EmployeeAPI.Services.EmployeeService.EmployeeService': Unable to resolve service for type 'EmployeeAPI.DataAccess.DataContext.EmployeeDBContext' while attempting to activate 'EmployeeAPI.Services.EmployeeService.EmployeeService'.)'

InvalidOperationException: Unable to resolve service for type 'EmployeeAPI.DataAccess.DataContext.EmployeeDBContext' while attempting to activate 'EmployeeAPI.Services.EmployeeService.EmployeeService'.

This is my Program.cs class

using EmployeeAPI.Services.EmployeeService;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

builder.Services.AddScoped<IEmployeeRepository, EmployeeService>();

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

This is my Service class

namespace EmployeeAPI.Services.EmployeeService
{
    public class EmployeeService : IEmployeeRepository
    {
        private readonly EmployeeDBContext _dbContext;
        private readonly IMapper _mapper;

        public EmployeeService(EmployeeDBContext dbContext, IMapper mapper)
        {
            _dbContext = dbContext;
            _mapper = mapper;
        }

        public async Task<ServiceResponse<List<GetEmployeeDto>>> GetEmployees()
        {
            var response = new ServiceResponse<List<GetEmployeeDto>>();
            var dbEmployees = await _dbContext.Employees.ToListAsync();
            response.Data = dbEmployees.Select(c => _mapper.Map<GetEmployeeDto>(c)).ToList();
            response.Message = "Successfully received all Employees Details";
            return response;
        }
    }
}

This is the Repository

namespace EmployeeAPI.Services.EmployeeService
{
    public interface IEmployeeRepository
    {
        public Task<ServiceResponse<List<GetEmployeeDto>>> GetEmployees();
    }
}

This is the Controller class

namespace EmployeeAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly EmployeeService _employeeService;

        public EmployeeController(EmployeeService employeeService)
        {
            _employeeService = employeeService;
        }

        [HttpGet]
        public async Task<ActionResult<ServiceResponse<List<GetEmployeeDto>>>> GetEmployees()
        {
           return Ok(await _employeeService.GetEmployees());
        }
    }
}

CodePudding user response:

You have to add the following to your Program.cs:

builder.Services.AddDbContext<EmployeeDBContext>(
    options => ...); // configute DbContext

Documentation

  • Related