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.

Passing arguments to next in pipeline behaviors

See original GitHub issue

Currently, the next parameter in IPipelineBehavior<TRequest, TResponse>.Handle is a RequestHandlerDelegate<TResponse> which takes no argument.

This is a problem, because it means it’s impossible for a behavior to pass anything other than the original request to the next behavior.

Here are two examples where it could be useful to be able to do that:

  • Applying a transformation to the request before it’s passed to the rest of the pipeline. It’s possible to modify the current one, but that works only if the request is mutable.
  • Passing a different cancellation token. For instance, I’d like to pass HttpContext.RequestAborted if no cancellation token was passed explicitly.

I realize this would be a breaking change, but maybe in a future major version?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lilasquaredcommented, May 28, 2020

Of course, you could introduce a new interface for behaviors, but it would probably make things confusing…

yeah this is what I was thinking but I can see how it would get cumbersome.

This has been discussed before in a few other issues as well I think, here is one with a response from the author https://github.com/jbogard/MediatR/issues/361

A mutable dependency on both the pipeline(s) and the handler seems to be the suggested approach.

1reaction
thomaslevesquecommented, May 28, 2020

Hi @lilasquared,

It wouldn’t necessarily be a breaking change if the library continued to support calling the delegate with no args.

How would you do that? The interface has to change anyway. RequestHandlerDelegate<TResponse> would need an additional TRequest generic type parameter, so it would no longer be the same type, so existing implementations would be broken.

Of course, you could introduce a new interface for behaviors, but it would probably make things confusing…

or is it more the principle of having immutable requests?

Yes, that. My requests are often immutable.

For your second, I remember a conversation about cancellation token in another issue…#496 the solution here was to wire up a new mediator that used the request aborted cancellation token if none was provided.

Yes, I thought of doing this too. It’s not a big deal, but I thought a behavior would be a really good fit for this, so I was a little disappointed when I realized it wasn’t possible.

Actually, since the handlers and behaviors are bound by type to the requests themselves, the “transformation” you’re talking about would have to transform it to the same request type anyway

Yes, I’m aware of this limitation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manipulate pipeline with behaviors • NServiceBus
At runtime, the pipeline will call the Invoke method of each registered behavior passing in as arguments the current message context and an ......
Read more >
Feature spotlight: Passing parameters between actions | Buddy
The 'Pass arguments' action in Buddy lets you ask about parameters during pipeline execution.
Read more >
Advanced features of the MediatR package - Pipeline ...
Each of the pipeline behaviors can implement a single cross-cutting ... and their Handle method has an extra delegate parameter called next.
Read more >
Pipeline execution and triggers - Azure Data Factory & ...
Pipeline runs are typically instantiated by passing arguments to parameters that you define in the pipeline. You can execute a pipeline ...
Read more >
Pipeline parameters and variables - Azure Data Factory & ...
Pipeline parameters can be used to control the behavior of a pipeline and its activities, such as by passing in the connection details...
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