MediatR icon indicating copy to clipboard operation
MediatR copied to clipboard

PipelineBehavior exception with wrapper on TResponse

Open defunky opened this issue 3 years ago • 1 comments

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.

defunky avatar Jun 29 '21 17:06 defunky

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.

Badabum avatar Aug 19 '21 07:08 Badabum