1 year ago
#378722
Rafa
Registering commands and handlers with generic interfaces
I'm studying about the mediator pattern. I would like to register a handler to handle a command that has a generic type, where the type is an IMessage
.
internal class NewEmployeeCommand<TMessage> : IRequest
where TMessage : IMessage
{
public string Name { get; init; }
}
internal class NewEmployeeCommandHandler<TMessage>
: IRequestHandler<NewEmployeeCommand<TMessage>>
where TMessage : IMessage
{
public async Task<Unit> Handle(
NewEmployeeCommand<TMessage> request, CancellationToken cancellationToken)
{
/* DO SOMETHING */
return Unit.Value;
}
}
When trying to send my command using await mediator.Send(command)
, I'm getting the following exception:
Error constructing handler for request of type MediatR.IRequestHandler. Register your handlers with the container.
I've already tried to configure dependency injection in several ways, but all without success:
services.AddTransient<IRequestHandler<NewEmployeeCommand<IDevMessage>>,
NewEmployeeCommandHandler<IDevMessage>>()
services.AddTransient(
typeof(IRequestHandler<NewEmployeeCommand<IDevMessage>>),
typeof(NewEmployeeCommandHandler<IDevMessage>))`
Can anyone help me where I'm going wrong? I'm using ASP.NET Core 6.
c#
.net
asp.net-core
dependency-injection
mediatr
0 Answers
Your Answer