ERROR:
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: BookACar.Application.Interfaces.CarInterfaces.ICarRepository Lifetime: Scoped ImplementationType: CarRepository’: Implementation type ‘CarRepository’ can’t be converted to service type ‘BookACar.Application.Interfaces.CarInterfaces.ICarRepository’) (Error while validating the service descriptor ‘ServiceType: BookACar.Application.Features.CQRS.Handlers.CarHandlres.GetCarWithBrandQueryHandler Lifetime: Scoped ImplementationType: BookACar.Application.Features.CQRS.Handlers.CarHandlres.GetCarWithBrandQueryHandler’: Implementation type ‘CarRepository’ can’t be converted to service type ‘BookACar.Application.Interfaces.CarInterfaces.ICarRepository’)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Program.<Main>$(String[] args) in C:ProjectsCarBookPresentationBookACar.WebApiProgram.cs:line 51This exception was originally thrown at this call stack:
[External Code]Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor ‘ServiceType: BookACar.Application.Interfaces.CarInterfaces.ICarRepository Lifetime: Scoped ImplementationType: CarRepository’: Implementation type ‘CarRepository’ can’t be converted to service type ‘BookACar.Application.Interfaces.CarInterfaces.ICarRepository’Inner Exception 2:
ArgumentException: Implementation type ‘CarRepository’ can’t be converted to service type ‘BookACar.Application.Interfaces.CarInterfaces.ICarRepository’
ICarRepository Interface:
namespace BookACar.Application.Interfaces.CarInterfaces
{
public interface ICarRepository
{
List<Car> GetCarsListWithBrand();
}
}
**
CarRepository class**
namespace BookACar.Persistence.Repositories.CarRepositories
{
public class CarRepository : ICarRepository
{
private readonly CarBookContext _context;
public CarRepository(CarBookContext context)
{
_context = context;
}
public List<Car> GetCarsListWithBrand()
{
var values = _context.Cars.Include(x => x.Brand).ToList();
return values;
}
}
}
program.cs
using BookACar.Application.Features.CQRS.Handlers.AboutHandlers;
using BookACar.Application.Features.CQRS.Handlers.BannerHandlres;
using BookACar.Application.Features.CQRS.Handlers.BrandHandlers;
using BookACar.Application.Features.CQRS.Handlers.CarHandlres;
using BookACar.Application.Interfaces;
using BookACar.Application.Interfaces.CarInterfaces;
using BookACar.Persistence.Repositories;
using BookACar.Persistence.Repositories.CarRepositories;
using CarBook.Persistence.CarBookContext;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<CarBookContext>();
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddScoped(typeof(ICarRepository), typeof(CarRepository));
builder.Services.AddScoped<GetCarQueryHandler>();
builder.Services.AddScoped<GetCarByIdQueryHandler>();
builder.Services.AddScoped<CreateCarCommandHandler>();
builder.Services.AddScoped<UpdateCarCommandHandler>();
builder.Services.AddScoped<RemoveCarCommandHandler>();
builder.Services.AddScoped<GetCarWithBrandQueryHandler>();
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();
I tried all ways but I can’t fix the problem. Why I am getting this error and how I can fix
Soner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.