MediatR
MediatR copied to clipboard
PipelineBehavior exception with wrapper on TResponse
I have a pipeline behavior as follows:
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, ResponseModel<TResponse>>
where TRequest : IRequest<TResponse>
with command and handler:
public class TestCommand : IRequest<TestResponse>
public class TestHandler : IRequestHandler<TestCommand, TestResponse>
results in exception being thrown:
'ValidationBehaviour`2[Commands.TestCommand,Entities.TestResponse]' can't be converted to service type 'MediatR.IPipelineBehavior`2[Commands.TestCommand,Entities.TestResponse]''
This should work fine, I can't seem what the issue is. The exception seems to be thrown before even reaching the Handle
method within the pipeline. I'm trying to avoid any reflection or hacky workarounds as I feel like this should just work.
This won't work if you're using native .net core di(Microsoft.Extensions.DependencyInjection
) and registering your behavior like this: services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>))
. Unfortunately, native di doesn't support this scenario. You can try Autofac
or other containers.