-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add access to services for route handler filter factories to RouteHandlerContext #41900
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
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
- Added `public IServiceProvider? Services { get; }` property to `RouteHandlerContext` - Enables route handler filter factories to access app services - Added a unit test & updated another to ensure app's service provider is passed through correctly by `RequestDelegateFactory` Fixes #41900
In |
Yep. |
From our email thread,
namespace Microsoft.AspNetCore.Http;
public sealed class RouteHandlerContext
{
+ public IServiceProvider? ApplicationServices { get; }
}
namespace Microsoft.AspNetCore.Http.Metadata;
public sealed class EndpointMetadataContext
{
- public IServiceProvider? Services { get; }
+ public IServiceProvider? ApplicationServices { get; }
} |
Sounds fair to me. |
From @davidfowl's email:
That's a good point. At first, I thought you were suggesting we lie about the nullability, but it occurs to me that we can just provide an "empty" |
Agreed. Fixed in the PR. |
Updated API proposal: namespace Microsoft.AspNetCore.Http;
public sealed class RouteHandlerContext
{
- public RouteHandlerContext(MethodInfo methodInfo, EndpointMetadataCollection endpointMetadata);
+ public RouteHandlerContext(MethodInfo methodInfo, EndpointMetadataCollection endpointMetadata, IServiceProvider applicationServices);
+ public IServiceProvider ApplicationServices { get; }
}
namespace Microsoft.AspNetCore.Http.Metadata;
public sealed class EndpointMetadataContext
{
- public EndpointMetadataContext(MethodInfo method, IList<object> endpointMetadata, IServiceProvider? services);
+ public EndpointMetadataContext(MethodInfo method, IList<object> endpointMetadata, IServiceProvider applicationServices);
- public IServiceProvider? Services { get; }
+ public IServiceProvider ApplicationServices { get; }
}
public sealed class EndpointParameterMetadataContext
{
- public EndpointParameterMetadataContext(ParameterInfo parameter, IList<object> endpointMetadata, IServiceProvider? services);
+ public EndpointParameterMetadataContext(ParameterInfo parameter, IList<object> endpointMetadata, IServiceProvider applicationServices);
- public IServiceProvider? Services { get; }
+ public IServiceProvider ApplicationServices { get; }
} |
API Review Note:
|
…41952) - Added `public IServiceProvider ApplicationServices { get; }` property to `RouteHandlerContext` - Enables route handler filter factories to access app services - Renamed existing `Services` property on `EndpointParameterMetadataContext` and `EndpointMetadataContext` to `ApplicationServices` and made it non-nullable - Added a unit test & updated another to ensure app's service provider is passed through correctly by `RequestDelegateFactory` Fixes #41900
…otnet#41952) - Added `public IServiceProvider ApplicationServices { get; }` property to `RouteHandlerContext` - Enables route handler filter factories to access app services - Renamed existing `Services` property on `EndpointParameterMetadataContext` and `EndpointMetadataContext` to `ApplicationServices` and made it non-nullable - Added a unit test & updated another to ensure app's service provider is passed through correctly by `RequestDelegateFactory` Fixes dotnet#41900
When adding a route handler filter via a filter factory there are cases where the filter factory (as opposed to the filter itself) needs access to the application services, e.g. to get an
ILogger
instance. We should add a property toRouteHandlerContext
to enable this.namespace Microsoft.AspNetCore.Http; public sealed class RouteHandlerContext { + public IServiceProvider ApplicationServices { get; } }
Note that the property is nullable due to where it's instantiated in
RequestDelegateFactory
and matches theServices
properties onEndpointMetadataContext
andEndpointParameterMetadataContext
.Example Usage
Risks
Should be very low. This is following the pattern used in other context classes including those where the call site originates in
RequestDelegateFactory
, e.g.EndpointMetadataContext
.The text was updated successfully, but these errors were encountered: