3

使用 Mediatr,我有以下请求处理程序:

public class GetEntityByIdRequest<TEntity> : IRequest<TEntity> where TEntity : Entity
{
    public int Id { get; set; }

    internal class Handler : IRequestHandler<GetEntityByIdRequest<TEntity>, TEntity>
    {
        public TEntity Handle(GetEntityByIdRequest<TEntity> message)
        {
            return new Session.Query<TEntity>().FirstOrDefault(x => x.Id == message.Id);
        }
    }
}

我在我的 IoC 中注册此通用请求处理程序时遇到问题。我试过像这样注册:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>));
container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<Entity>));

这给了我错误:

System.ArgumentException:提供的类型 GetEntityByIdRequest<TEntity> 未实现 IRequestHandler<TRequest, TResponse>。参数名称:服务类型

我也看过与我遇到的问题相同,但该人使用的是 StructureMap 而不是 Simple Injector。

有人可以帮我注册我的通用请求处理程序。

4

1 回答 1

2

您正在将查询类型注册为处理程序。这显然行不通。您将不得不注册处理程序:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>.Handler));
于 2017-11-06T17:28:31.647 回答