jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.91k stars 1.16k forks source link

IRequestExceptionHandler on different project does not get triggered #933

Closed fh-andri closed 11 months ago

fh-andri commented 1 year ago

Hi,

I have an API solution with 2 projects and using MediatR 12.1.1. First project has MediatR preprocessor and the exception handler and the other one has the request handler. The preprocessor is working fine even it's on separate project with the request handler but not with the exception handler. The exception does not trigger the handler if I put the exception handler class separately with the request handler. But if I put the exception handler class in same project with the request handler, it works. What would be the issue I have here?

public class PipelinePreProcessor<TRequest> : IRequestPreProcessor<TRequest> where TRequest : notnull
{
    private readonly IValidator<TRequest> _validator;

    public PipelinePreProcessor(IValidator<TRequest> validator)
    {
        _validator = validator;
    }
    public Task Process(TRequest request, CancellationToken cancellationToken)
    {
        //Request validation
        var validationResult = Task.Run(() =>_validator.ValidateAsync(request, cancellationToken)).GetAwaiter().GetResult();
        if (!validationResult.IsValid)
        {
            throw new ValidationException(validationResult.Errors);
        }
        return Task.CompletedTask;
    }
}
public class ValidationExceptionHandler<TRequest, TResponse, TException> : IRequestExceptionHandler<TRequest, TResponse, TException>
    where TRequest : notnull
    where TException : ValidationException
{
    public Task Handle(TRequest request, TException exception, RequestExceptionHandlerState<TResponse> state, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Thank you!

remcoros commented 1 year ago

You miss something in your registration code (which you didn't post here).

https://github.com/jbogard/MediatR/wiki first Paragraph: Setup

make sure to register the assemblies of your other projects too.

fh-andri commented 1 year ago

@remcoros I have added the setup on both projects to DI. Both project are having MediatR installed as well. Here is the code

services.AddMediatR(cfg =>
        {
            cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
            cfg.AddRequestPreProcessor(typeof(IRequestPreProcessor<>), typeof(PipelinePreProcessor<>));
            cfg.AddRequestPostProcessor(typeof(IRequestPostProcessor<,>), typeof(PipelinePostProcessor<,>));
        });
remcoros commented 11 months ago

You are using Assembly.GetExecutingAssembly(). That only scans the current assembly, not the assemblies of other projects. That should be fixed to pick up types from your other projects as well

fh-andri commented 11 months ago

@remcoros Thank you! I was not realize it and thinking all registered correctly because of the pipeline is working. Have a good day!