Pass over the handler instance to the middleware
See original GitHub issueSometimes 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:
- Created 5 years ago
- Comments:13 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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.