-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Best practice for a singleton route handler filter with Minimal APIs? #41259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for contacting us. We're moving this issue to the |
Some of what you're describing here feels like the "global filters" concept that we discussed during the design of this feature. At the time, we concluded that we didn't want to build a design that explicitly favored global filters and relegated route groups as the many approach for solving "apply the same filter to multiple routes." After all, it would stand to reason that if you wanted to apply the same filter to multiple routes then they can be conceptually placed in a group. |
We could make a tweak to
Yes |
@martincostello Have you had the chance to use groups + filters in the latest preview7 release? Do you think that's a good solution to the problem you're having? |
Yes, I had a quick play around with them. In this sample app I added route groups to apply the common metadata for all the endpoints, including applying the previously repeated filter: martincostello/polly-rate-limiting@cdc74e4 I think for what I've done with it, it solves the duplication, thought I still feel it's a bit of an outlier compared to the rest of Minimal APIs that it's not resolved from DI as a service if registered as one. That's not necessarily an issue with the route groups as it ultimately solves the use case, it just feels a bit different/surprising compared to the overall Developer Experience for using/consuming services with 6.0. |
Thanks for contacting us. We're moving this issue to the |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
The new support for route handler filters for Minimal APIs currently supports these use cases:
IRouteHandlerFilter
instance specified when the route is mapped.T
that implementsIRouteHandlerFilter
per HTTP request and invoke it. The filter is created with access to theIServiceProvider
so can use types registered withIServiceCollection
, but the filter itself is not resolved from the DI container.RouteHandlerContext
andRouteHandlerFilterDelegate
.In the case that a filter can logically be re-used between multiple endpoints as a singleton but cannot be specified directly when mapping the request delegates (item 1), what is the recommended best practice here with Minimal APIs in ASP.NET Core 7?
An application could do this, but it seems a bit verbose to re-use the same filter instance compared to option 2, except that creates a new instance of the filter for each invocation.
Describe the solution you'd like
What's the recommendation on how to author the application for this use-case where minimising the creation of
IRouteHandlerFilter
instances is desired but it cannot be created up-front when registering the routes?I thought I'd raise the question about this, as it seems to be a bit of an outlier compared to the rest of Minimal APIs where trying to use a service will get it from DI, otherwise try and parse/bind it or deserialize it from the request body. The
AddFilter<T>()
method seems to be halfway in between, being able to leverage DI without being in DI itself.Additional context
No response
The text was updated successfully, but these errors were encountered: