question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IRequestPreProcessor not called before IPipeLineBehavior

See original GitHub issue

Hi there,

I’m having some difficulties migrating my code to version 12.0.1.

I have a RequestPreProcessor defined as

public class RequestContextPreProcessor<TRequest> : IRequestPreProcessor<TRequest>  
where TRequest : notnull, IHomeControllerRequest

and a number of Behaviors which I register using the follwoing code:

services.AddMediatR(config => {
                        config.RegisterServicesFromAssemblies(PluginProvider.GetAssemblies());
                        config.AddOpenBehavior(typeof(TelemetryBehavior<,>));
                        config.AddOpenBehavior(typeof(LoggingBehavior<,>));
                        config.AddOpenBehavior(typeof(ErrorHandlingBehavior<,>));
                    })

I can’t get the RequestPreProcessor to execute. For some reason, it’s not being registered and injected as part of the pipeline. My code worked fine with version 11. I tried explicitly registering the RequestPreProcessor, but that did not work either.

Any suggestions?

Thanks Alexander

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
AleRoecommented, Mar 18, 2023

Hi @jbogard! Thanks for getting back and the explanations. Funny thing though is that I do have an IRequestPreProcessor implementation in the main assembly, but its Process() method does not get called (the class is being loaded though, because the constructor is called). The only way to get it to work is by manually adding the RequestPreProcessorBehavior<,> prior to any other custom behaviors.

So this works:

services.AddMediatR(config => {
                        config.RegisterServicesFromAssemblies(PluginProvider.GetAssemblies());
                        config.AddOpenBehavior(typeof(RequestPreProcessorBehavior<,>));
                        config.AddOpenBehavior(typeof(TelemetryBehavior<,>));
                        config.AddOpenBehavior(typeof(LoggingBehavior<,>));
                        config.AddOpenBehavior(typeof(ErrorHandlingBehavior<,>));
                    });

Also, my code worked fine with v11.

I can live with manually adding the RequestPreProcessorBehavior, but it does seem strange and not how it’s supposed to work. So I did some more testing:

And as it turns out, my IRequestPreProcessor is being called, but in the wrong order. It gets called after my custom IPipeLineBehaviors, instead of before. I verified this by first removing all my custom behaviors and then by adding only a single custom beviour that does not depend on the IRequestPreProcessor, and then checking the call-order using breakpoints.

So it seems that the built in pipeline discovery is adding the behaviors in the wrong order, at least when configuring AddMediatR() the way I do it (see my first post please).

Hope this helps. Kind regards Alexander

2reactions
jbogardcommented, Jul 6, 2023

I’m going to address this in the next release, to put request pre/post processors before behaviors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

.net - When should I use IPipelineBehavior over ...
They all get executed before the handler which is what I want them to do anyway. So my question is when would I...
Read more >
Sharing Context in MediatR Pipelines
Basically, I'm recreating a lot of existing functional patterns in an OO language, using dependency injection. Side note - it's possible to do ......
Read more >
How I Upgrade my Code-Style of MediatR Pipeline using . ...
I feel the MediatR attributes is missing the point of a behavior middleware, which is to handle common, cross-cutting concerns.
Read more >
MediatR Behaviors
Looking at some of the new changes in MediatR v3. Including behaviors that enable you to create your own custom request pipelines.
Read more >
Logging pipeline with MediatR
Before each request, all pipeline behaviours are called, if there are any, which wrap requests. This is what makes this blog post possible....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found