I created a request exception handler with known types as below and the exception is handled. The only examples I found are doing it this way.
using MediatR.Pipeline;
using Microsoft.Extensions.Logging;
using MyPortal.Application.Common.Models;
using MyPortal.Application.MyCommands.Commands.CompleteMyCommand;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MyPortal.Application.Common.Behaviors
{
public class RequestGenericExceptionHandler : IRequestExceptionHandler<MyCommand, Result>
{
private readonly ILogger<MyCommand> _logger;
public RequestGenericExceptionHandler(ILogger<MyCommand> logger)
{
_logger = logger;
}
public async Task Handle(MyCommand request,
Exception exception,
RequestExceptionHandlerState<Result> state,
CancellationToken cancellationToken)
{
var name = typeof(MyCommand).Name;
_logger.LogError("MyPortal Request Exception {@Request}",
name, exception.Message, request);
}
}
}
When I try to use something like this it's not handled:
public class RequestGenericExceptionHandler<TRequest, TResponse> : IRequestExceptionHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<TRequest> _logger;
public RequestGenericExceptionHandler(ILogger<TRequest> logger)
{
_logger = logger;
}
public async Task Handle(TRequest request,
Exception exception,
RequestExceptionHandlerState<TResponse> state,
CancellationToken cancellationToken)
{
var name = typeof(TRequest).Name;
_logger.LogError("MyPortal Request Exception {@Request}",
name, exception.Message, request);
}
}
I'm using .Net Core 3.1, Mediator 8.0.1, and MediatR.Extensions.Microsoft.DependencyIntjection 8.0.0 and this is my only dependency injection:
services.AddMediatR(Assembly.GetExecutingAssembly());
I needed to make these changes:
This line is only needed if not using
MediatR.Extensions.Microsoft.DependencyIntjection
:Reference: https://github.com/jbogard/MediatR/issues/486