fingers10
fingers10

Reputation: 7927

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?

Upvotes: 29

Views: 17572

Answers (2)

sam
sam

Reputation: 340

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

Upvotes: 3

msmolcic
msmolcic

Reputation: 6557

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

Upvotes: 82

Related Questions