Skip to content

EndpointBuilder.ApplicationServices should use init instead of set #43544

@halter73

Description

@halter73

Background and Motivation

When doing more work with EndpointBuilder as part of #43543, I noticed that it was odd and unnecessary that ApplicationServices could be modified after construction. Using init doesn't completely prevent this, but it makes it clear it's unintended.

I also think that IEndpointMetadataProvider and IEndpointMetadataProvider belong in Http.Abstractions instead of Http.Extensions. It doesn't have any big dependency, and we already have all our other endpoint metadata related stuff there like EndpointBuilder itself.

Proposed API

namespace Microsoft.AspNetCore.Builder;
 
public abstract class EndpointBuilder
{
-    public IServiceProvider ApplicationServices { get; set; } = EmptyServiceProvider.Instance;
+    public IServiceProvider ApplicationServices { get; init; } = EmptyServiceProvider.Instance;
}
- Microsoft.AspNetCore.Http.Extensions.dll
+ Microsoft.AspNetCore.Http.Abstractions.dll

namespace Microsoft.AspNetCore.Builder;

public interface IEndpointMetadataProvider
{
     static abstract void PopulateMetadata(MethodInfo method, EndpointBuilder builder);
}

public interface IEndpointParameterMetadataProvider
{
     static abstract void PopulateMetadata(ParameterInfo parameter, EndpointBuilder builder);
}

Usage Examples

var builder = new RouteEndpointBuilder(requestDelegate, updatedRoutePattern, route.Order)
{
    DisplayName = action.DisplayName,
    ApplicationServices = _serviceProvider,
};

Alternative Designs

Leave it as set. Leave the providers in Http.Extensions.dll.

Risks

I think it's riskier to leave it as set. It's new API, and we could always remove the init-onlyness in a later release. I don't think it's a huge problem either way though.

If someone wanted to enlighten an existing EndpointBuilder they didn't construct, the C# language will now try to stop them from modifying it. This might be annoying, but changing this value after construction could lead to some weird hard-to-diagnose bugs.

I cannot think of any justification for not moving the providers to Http.Abstractions.dll.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions