jbogard / MediatR

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

BUG - register open generic pipeline #846

Closed don-flamingo closed 1 year ago

don-flamingo commented 1 year ago

Hey!

I have pulled the newest 12.00 MediatR version into my modular monolith application. Previously, I registered my validation pipeline as the code:

    public static IServiceCollection AddPipelinesModule(this IServiceCollection serviceCollection) =>
        serviceCollection.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationPipeline<,>));

After migration, the validation doesn't work correctly. I have checked the lib methods and transformed the upper piece of code into

builder.Services
    .AddMediatR(cfg =>
    {
        cfg.AddOpenBehavior(
            typeof(ValidationPipeline<,>));
        cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
    });

But still without success.

Btw. when I have skipped RegisterServicesFromAssembly(typeof(Program).Assembly); the library throws an exception - no assemblies to scan. It's problematic because a program is only a shared entry point to my app.

My app looks like this: WebApi - Controllers, Entrypoint, ModuleA - Logic for A, Command handlers, ModuleB - logic for B, Bommand handlers, etc.

marcantoinecouture commented 1 year ago

I have the same bug in my application.

jbogard commented 1 year ago

Have a repro? The tests look fine here.

don-flamingo commented 1 year ago

Looks like it happens when I have IRequestHandler implementation without returning anything.

don-flamingo commented 1 year ago

Ok, I Found it. It happens when Pipeline implementation looks like

public class ValidationPipeline<TRequest, TResponse> :
    IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>

When I changed into

public class ValidationPipeline<TRequest, TResponse> :
    IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest

Void pipelines works correctly, but.... When handler return something, in the upper case not working.

So, currently to hack it, I must have 2 pipelines instead of 1.

jbogard commented 1 year ago

Did you follow the upgrade guide?

jbogard commented 1 year ago

I added a big bold message to also correct your behaviors:

https://github.com/jbogard/MediatR/wiki/Migration-Guide-11.x-to-12.0

That will fix your issue.