The Wayback Machine - https://web.archive.org/web/20220201200133/https://github.com/dotnet/aspnetcore/issues/39921
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 EndpointMetadataCollection.GetRequiredMetadata #39921

Open
JamesNK opened this issue Feb 1, 2022 · 0 comments
Open

Add EndpointMetadataCollection.GetRequiredMetadata #39921

JamesNK opened this issue Feb 1, 2022 · 0 comments

Comments

@JamesNK
Copy link
Member

@JamesNK JamesNK commented Feb 1, 2022

Background and Motivation

EndpointMetadataCollection has a GetMetadata<T>() method:

public T? GetMetadata<T> () where T : class;

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.endpointmetadatacollection.getmetadata?view=aspnetcore-6.0#microsoft-aspnetcore-http-endpointmetadatacollection-getmetadata-1

It returns null if there is no matching metadata. It would be useful to have a GetRequiredMetadata<T>() method that returns a value or throws.

Proposed API

namespace Microsoft.AspNetCore.Http
{
    public class EndpointMetadataCollection
    {
+       public T GetRequiredMetadata<T> () where T : class;
    }
}

The method would function the same as getting a required service from DI or a required feature from a feature collection.

Usage Examples

endpoints.MapGet("/", context =>
{
    var endpoint = context.GetEndpoint();
    var requiredMetadata = endpoint.Metadata.GetRequiredMetadata<MyMetadata>();
});

Alternative Designs

Risks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant