Some services are not able to be constructed in ASP.NET Core

sliziky

having hard time with CQRS because of this exception. I have a Movie model and also MovieDTO model

//EDIT Okay I've just realized that in GetMoviesQuery I don't use IRepository< MovieDTO > and when I change MovieRepository to IRepository<MovieDTO> then it works

IRepository.cs

public interface IRepository<TEntity>
{
    IEnumerable<TEntity> GetAll();

    TEntity Get(int id);

    TEntity Save(TEntity entity);

    void Delete(int entityId);
}

MovieRepository.cs

public class MovieRepository : IRepository<MovieDTO>
{
    private MyContext _context;
    private IMapper _mapper;
    public MovieRepository(MyContext context)
    {
        _context = context;
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<Movie, MovieDTO>();
        });
        _mapper = config.CreateMapper();
    }

    public IEnumerable<MovieDTO> GetAll()
    {
        return _context.Movies.ToList().Select(movie => _mapper.Map<Movie, MovieDTO>(movie));
    }
}

GetMoviesQuery.cs

public class GetMoviesQuery
{
    public class Query : IRequest<IEnumerable<MovieDTO>> { }

    public class Handler : RequestHandler<Query, IEnumerable<MovieDTO>>
    {
        private MovieRepository _repository;

        public Handler(MovieRepository repository)
        {
            _repository = repository ?? throw new ArgumentNullException(nameof(_repository)); 
        }

        protected override IEnumerable<MovieDTO> Handle(Query request)
        {
            return _repository.GetAll();
        }
    }
}

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddRazorPages();
        services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
        services.AddHttpClient();
        services.AddDbContext<MyContext>(options => options.UseSqlite("Data Source = blogging.db"));
        services.AddMediatR(typeof(Startup));
    }

Exception: System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler2[TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Query,System.Collections.Generic.IEnumerable1[TicketReservationSystem.Server.Models.DTO.MovieDTO]] Lifetime: Transient ImplementationType: TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Handler': Unable to resolve service for type 'TicketReservationSystem.Server.Data.Repository.MovieRepository' while attempting to activate

I have no idea how to actually find out where's the problem.

Yegor Androsov

public Handler(MovieRepository repository)

should be changed to

public Handler(IRepository<MovieDTO> repository), since you registered your container with interface, not implementation.

services.AddTransient<IRepository<MovieDTO>, MovieRepository>();

If you want to use your original code, register class itself instead

services.AddTransient<MovieRepository>();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Core - AggregateException: some services are not able to be constructed

Some services are not able to be constructed in .net core api

ASP.NET Core Web API -Some services are not able to be constructed: Error while validating the service descriptor ServiceType

System.AggregateException: 'Some services are not able to be constructed' In my ASP.net core

Some services are not able to be constructed error while validating the service descriptor in ASP.NET Core Web API

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

ASP.NET Core Web App DI Error - Some services are not able to be constructed (Error while validating the service descriptor

Some services are not able to be constructed

Some services are not able to be constructed in a AuthorizationHandler

Blazor Error - Some services are not able to be constructed

ERROR DI (AggregateException: Some services are not able to be constructed)

System.AggregateException: 'Some services are not able to be constructed

System.AggregateException: Some services are not able to be constructed

Factory method and Dependency injection, Some services are not able to be constructed

System.AggregateException: 'Some services are not able to be constructed' in Blazor app

Error Some services are not able to be constructed (Error while validating the service descriptor )

Some services are not able to be constructed === When I tend to build my API

'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:IHostedService

Some services are not able to be constructed (Error while validating the service descriptor) C#

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Restaurant.Data.IAppRepository

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:

Some services are not able to be constructed & Unable to resolve service for type while attempting to activate

Mocking Services in ASP.NET Core

Registering MVC services in ASP .NET Core 6

ASP.NET Core 6 app not able to find UseWindowsService

asp.net core 2.0 not able to post a simple form

Not able to redirect to action when using TempData in Asp.Net Core

Published Asp.Net Core App not able to save to wwwroot

Asp.Net Core - Not Able to Upload Files From Dropzone JS

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive