My command:
public class Command : IRequest { ... }
My handler:
public class CommandHandler : IAsyncRequestHandler<Command> { ... }
My pipeline registration (not using open generics):
services.AddTransient<IPipelineBehavior<Command>, MyBehavior<Command>>();
However this doesn't work: Using the generic type 'IPipelineBehavior<TRequest, TResponse>' requires 2 type arguments.
And same error for MyBehavior
.
The docs mention the Unit
struct. How do I use it?
As Mickaël Derriey pointed out, MediatR already defines IRequest, IRequestHandler and IAsyncRequestHandler to not return a value if it isn't needed.
If you look at IRequest, you can see it actually inherits from
IRequest<Unit>
, which means when you processCommand
, your pipeline behaviorMyBehavior
will return theUnit
struct as the response by default without needing to specify an explicit response for yourCommand
.As an example:
I think I've figured it out, and it seems to work so far.