MediatR IPipelineBehavior<TRequest, TResponse> errors as The type 'TRequest' cannot be used as type parameter 'TRequest' in the generic type or method
Asked Answered
W

2

28

I'm using MediatR to do Request - Response logging in my application using IPipelineBehavior<TRequest, TResponse>

Code Sample:

internal sealed class AppLoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    private readonly ILogger<AppLoggingBehavior<TRequest, TResponse>> _logger;

    public AppLoggingBehavior(ILogger<AppLoggingBehavior<TRequest, TResponse>> logger)
    {
        _logger = logger;
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        string requestName = typeof(TRequest).Name;
        string unqiueId = Guid.NewGuid().ToString();
        string requestJson = JsonSerializer.Serialize(request);
        _logger.LogInformation($"Begin Request Id:{unqiueId}, request name:{requestName}, request json:{requestJson}");
        var timer = new Stopwatch();
        timer.Start();
        var response = await next();
        timer.Stop();
        _logger.LogInformation($"End Request Id:{unqiueId}, request name:{requestName}, total request time:{timer.ElapsedMilliseconds}ms");
        return response;
    }
}

But After upgrading to Nuget - v10.0.0 I started getting the below compilation error.

The type 'TRequest' cannot be used as type parameter 'TRequest' in the generic type or method 'IPipelineBehavior<TRequest, TResponse>'. There is no boxing conversion or type parameter conversion from 'TRequest' to 'MediatR.IRequest'

I managed to find the porting guide from official MediatR repo. But couldn't find any examples.

Am I missing something else, Please can anyone assist me on this?

Willing answered 10/1, 2022 at 14:53 Comment(2)
The porting guide is very obvious about what should be done, just add the green text from the porting guide (where TRequest : IRequest<TResponse>) to your class declaration making your entire class declaration internal sealed class AppLoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>Alhambra
Thanks for the inputs. I misunderstood the example in porting guide as git diff.Willing
F
80

You need to specify the type of your TRequest parameter in your abstract class as well. It has to be at least specific as the parameter in the interface you're trying to implement.

internal sealed class AppLoggingBehavior<TRequest, TResponse>
    : IPipelineBehavior<TRequest, TResponse>
    where TRequest : MediatR.IRequest<TResponse> // <- this is the part you're missing
{
    // rest of your code...
}
Froze answered 10/1, 2022 at 14:57 Comment(1)
In v12 this has been reverted to where TRequest : notnull. See the migration guide.Hordein
E
3

According to the Github docs, this is what is required https://github.com/jbogard/MediatR/wiki/Migration-Guide-9.x-to-10.0

where TRequest : IRequest

Entablature answered 21/6, 2022 at 21:3 Comment(1)
aaaaaand, in V12 this has been reverted....Hordein

© 2022 - 2024 — McMap. All rights reserved.