【问题标题】:Error constructing handler for request of type mediatr.irequesthandler`2为 mediatr.irequesthandler`2 类型的请求构造处理程序时出错
【发布时间】:2022-04-13 21:02:53
【问题描述】:

当我将 IMapper 注入 MediatR 命令处理程序时出现此问题。我在此之前正确注入,但之后更改了 AutoMapperMediatR 配置,如下所示。 (我添加了所有应用层的程序集)

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

当我执行 CreateAccountCommand 时出现如下错误:

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.

内部异常如下

Property 'System.Linq.IQueryable`1[Domain.Entities.Thumbnail] Thumbnails' is not defined for type 'Domain.Entities.Thumbnail' (Parameter 'property')

【问题讨论】:

标签: c# asp.net-core automapper mediatr


【解决方案1】:

我也遇到了这个问题,解决方法是在我的应用层添加如下代码,然后从Program调用它

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

我的程序代码调用顺序

builder.Services.AddMediatR(Assembly.GetExecutingAssembly())
builder.Services.AddApplication();

【讨论】:

    最近更新 更多