Search code examples
c#.netmediatr

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


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?


Solution

  • 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...
    }