使用 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。
有人可以帮我注册我的通用请求处理程序。