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.

Pass over the handler instance to the middleware

See original GitHub issue

Sometimes you want the actual instance of the handler in the middleware (Pipeline behaviour), for example to read class attributes or check if it implements a particular interface

Can the Mediatr be extended to pass the handler instance?

var handlerInstance = GetHandler<IRequestHandler<TRequest, TResponse>>(serviceFactory);
Task<TResponse> Handler() => handlerInstance.Handle((TRequest) request, cancellationToken);
            return serviceFactory
                .GetInstances<IPipelineBehavior<TRequest, TResponse>>()
                .Reverse()
                .Aggregate((RequestHandlerDelegate<TResponse>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next, handlerInstance))();

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, Jan 18, 2019

Mainly because it’s easier to take a dependency on IUserContext or similar than it is to have essentially traits/mixins with interfaces and relying on generic constraints to get things wired up correctly. You’ll have fewer headaches just supplying your handlers with dependencies than trying to have them wired up directly.

1reaction
jbogardcommented, Jan 15, 2019

If you’re trying to go the property injection route, setting state on your handlers themselves, I’d strongly caution you against that. At most, I’d set state on shared services, not on a handler itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing data from handler to middleware after serving ...
My real question that I am trying to solve is; how can I pass information from the handler to the middleware? Adding a...
Read more >
Migrate HTTP handlers and modules to ASP.NET Core ...
Order of middleware is based on the order in which they're inserted into the request pipeline, while order of modules is mainly based...
Read more >
Handlers and middleware in the AWS SDK for PHP Version 3
A middleware is a higher-order function that modifies a command, request, or result as it passes through the middleware. A middleware takes the...
Read more >
Handler and Middleware Design Pattern in Golang
Once the middleware is defined, you use it by literally wrapping it around the handler. eventHandler.Handle(event1, ExampleMiddleware(handler)).
Read more >
Middleware Patterns in Go
A middleware handler is simply an http.Handler that wraps another http.Handler to do some pre- and/or post-processing of the request. It's called "middleware" ......
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