I have a generic interface IPipelineBehavior<TRequest, TResponse>
(from MediatR). I'm trying to register a specific behavior for this interface as follows:
services.AddTransient(typeof(IPipelineBehavior<,>),
typeof(ValidationMiddleware<,>));
ValidationMiddleware
implements IPipelineBehavior
like so:
public class ValidationMiddleware<TRequest, TResponse>
: IPipelineBehavior<TRequest, Result<TResponse>>
Result
is a custom class I want all of my MediatR IRequestHandlers to return.
When the app runs, the service container gives me the following error:
System.ArgumentException: Implementation type 'StudentManagement.App.Middleware.ValidationMiddleware
2[StudentManagement.App.Students.DisenrollStudentCommand,StudentManagement.App.Result
1[System.Object]]' can't be converted to service type 'MediatR.IPipelineBehavior2[StudentManagement.App.Students.DisenrollStudentCommand,StudentManagement.App.Result
1[System.Object]]'
I can't see why because they clearly both have the same type arguments at runtime. My guess is that the Result<TResponse>
bit is causing the error for some reason, because other behaviors work fine when they just implement IPipelineBehavior<TRequest, TResponse>
.
Does anyone understand why I'm getting this error, and what I need to do to solve it?