Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error constructing handler for request of type mediatr.irequesthandler`2

This problem occurred when I inject IMapper into the MediatR command handler. I injected properly before this happen but after that changed AutoMapper and MediatR configs like bellow. (I added all assemblies of application layers)

services.AddAutoMapper(Assembly.GetExecutingAssembly(),
                               typeof(CreateAccountCommand).Assembly,
                               typeof(EfRepository<,>).Assembly,
                               typeof(IRepository<,>).Assembly,
                               typeof(KeyValueDto).Assembly);

        services.AddMediatR(Assembly.GetExecutingAssembly(),
                            typeof(CreateAccountCommand).Assembly,
                            typeof(EfRepository<,>).Assembly,
                            typeof(IRepository<,>).Assembly,
                            typeof(KeyValueDto).Assembly);

When I execute CreateAccountCommand the error occures like bellow:

Error constructing handler for request of type MediatR.IRequestHandler`2[Application.Actions.Users.Commands.CreateAccountCommand,Application.Common.Models.GenericResponse`1[System.String]]. Register your handlers with the container. See the samples in GitHub for examples.

Inner exception is bellow

Property 'System.Linq.IQueryable`1[Domain.Entities.Thumbnail] Thumbnails' is not defined for type 'Domain.Entities.Thumbnail' (Parameter 'property')
like image 504
Emba Ayyub Avatar asked Feb 05 '26 05:02

Emba Ayyub


1 Answers

I also encountered this problem, and the solution was to add the following code to my application layer,and then call it from Program

public static IServiceCollection AddApplication(this IServiceCollection services)
{
    var assembly = Assembly.GetExecutingAssembly();
    return services.AddMediatR(assembly);
}

My program code call order

builder.Services.AddMediatR(Assembly.GetExecutingAssembly())
builder.Services.AddApplication();
like image 92
Jiang Avatar answered Feb 07 '26 09:02

Jiang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!