Skip to content
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

Add Key Property to ServiceFilterAttribute #58017

Open
rjperes opened this issue Sep 22, 2024 · 0 comments
Open

Add Key Property to ServiceFilterAttribute #58017

rjperes opened this issue Sep 22, 2024 · 0 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@rjperes
Copy link
Contributor

rjperes commented Sep 22, 2024

Background and Motivation

Since .NET 8, the service provider supports keyed service registrations. However, the ServiceFilterAttribute, which uses dependency injection from a type, does not allow specifying an optional key for the registration. It should have an optional Key property.
Internally, the only change would be to check if the Key property is not null or whitespace, and then call GetRequiredKeyedService instead of GetRequiredService.

Proposed API

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("Type = {ServiceType}, Key = {Key}, Order = {Order}")]
public class ServiceFilterAttribute : Attribute, IFilterFactory, IOrderedFilter
{
    public ServiceFilterAttribute(Type type) {}
    public object? Key { get; set; }


    public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
    {
        ArgumentNullException.ThrowIfNull(serviceProvider);

        IFilterMetadata filter;

        if (Key is null || (Key is string keyStr && string.IsNullOrWhiteSpace(keyStr)))
        {
            filter = (IFilterMetadata)serviceProvider.GetRequiredService(ServiceType);
        }
        else
        {
            filter = (IFilterMetadata)serviceProvider.GetRequiredKeyedService(ServiceType, Key);
        }
        if (filter is IFilterFactory filterFactory)
        {
            // Unwrap filter factories
            filter = filterFactory.CreateInstance(serviceProvider);
        }

        return filter;
    }
}

Usage Examples

[ServiceFilter(type(MyActionFilter), Key = "My")]
public IActionResult Action() { ... }
@rjperes rjperes added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Sep 22, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

1 participant