My CQRS file layout is as in the picture. Whenever I enable the handler inside the GetAllBooks folder, I get an error.

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MediatR.IRequestHandler2[BookAPI.Application.Features.Queries.Book.GetAllBooks.GetAllBookQueryRequest,System.Collections.Generic.List1[BookAPI.Application.Features.Queries.Book.GetAllBooks.GetAllBookQueryResponse]] Lifetime: Transient ImplementationType: BookAPI.Application.Features.Queries.Book.GetAllBooks.GetAllBookQueryHandler': Unable to resolve service for type 'BookAPI.Application.Repositories.IBookReadRepository' while attempting to activate 'BookAPI.Application.Features.Queries.Book.GetAllBooks.GetAllBookQueryHandler'.)

GetAllBookQueryHandler

public class GetAllBookQueryHandler : IRequestHandler<GetAllBookQueryRequest, List<GetAllBookQueryResponse>>
    {
        private IBookReadRepository bookReadRepository;

        public GetAllBookQueryHandler(IBookReadRepository bookReadRepository)
        {
            this.bookReadRepository = bookReadRepository;
        }

        public async Task<List<GetAllBookQueryResponse>> Handle(GetAllBookQueryRequest request, CancellationToken cancellationToken)
        {
            List<B.Book> books = bookReadRepository.GetAll().Include(x=>x.Authors).Include(x=>x.Category).Include(x=>x.BookImages).ToList();
            List<GetAllBookQueryResponse> responses = new();
            foreach (B.Book book in books) 
            {
                responses.Add(
                    new GetAllBookQueryResponse 
                    {
                        Name= book.Name,
                        CategoryName=book.Category.Name,
                        AuthorName=book.Authors.First().Name,
                        Img=book.BookImages.First().Path,
                        UnitPrice=book.UnitPrice,
                    }
                    );
            }
            return responses;
        }
    }

GetAllBookQueryRequest

public class GetAllBookQueryRequest : IRequest<List<GetAllBookQueryResponse>>
    {
        //This place is empty as all books are requested
    }

GetAllBookQueryResponse

public class GetAllBookQueryResponse
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string CategoryName { get; set; }
        public string AuthorName { get; set; }
        public string Img { get; set; }
        public ushort UnitPrice { get; set; }

    }

ServiceRegistration for IoC

using MediatR;
using Microsoft.Extensions.DependencyInjection;


namespace BookAPI.Application
{
    public static class ServiceRegistration
    {
        public static void AddApplicationServices(this IServiceCollection services)
        {
            //find all handler, request and response and add IoC
            services.AddMediatR(typeof(ServiceRegistration));
            services.AddHttpClient();
        }

    }
}

Program.cs

I add services builder.Services.AddApplicationServices();

Book Controller

         .
         .
         .
        readonly IMediator mediator;
        public BookController(IBookWriteRepository bookWriteRepository, IWebHostEnvironment webHostEnvironment, IFileService fileService, IMediator mediator)
        {
            bookWriteRepository = bookWriteRepository;
            _fileService = fileService;
            this.mediator = mediator;
        }
        [HttpGet]
        public async Task<IActionResult> GetAllBooks([FromQuery] GetAllBookQueryRequest getAllBookQueryRequest)
        {
            return Ok(await mediator.Send(getAllBookQueryRequest));
        }
        .
        .
        .

I guess it doesn't see the service I introduced, but I don't understand why GetAllBookHandler is throwing an error in the operation and not the others. For example, my handlers that list and create customers are working.

0

There are 0 best solutions below