Emba Ayyub
Emba Ayyub

Reputation: 697

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')

Upvotes: 3

Views: 29594

Answers (3)

Debasish Adhikary
Debasish Adhikary

Reputation: 1

builder.Services.AddMediatR(typeof(MyQuery).GetTypeInfo().Assembly);

Upvotes: 0

Gauravsa
Gauravsa

Reputation: 6514

One way to find out the actual error is to open Event Viewer. In the Event Viewer, go to custom views > Summary Page Events.

Here you can see all the errors which are relevant to your code. In our case, error was coming like this:

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')

Upon checking the error, I was missing appsettings.json in the published folder. Due to the missing appsettings, connection string wasn't found and migration failed with the error:

Error constructing handler for request of type MediatR.IRequestHandler. 
Register your handlers with the container. See the samples in GitHub for examples

Upvotes: 0

Jiang
Jiang

Reputation: 11

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();

Upvotes: 1

Related Questions