Skip to content

EndpointBuilder.ServiceProvider should be non-nullabe and renamed to ApplicationServicesย #42137

Closed
@halter73

Description

@halter73

Background and Motivation

We missed this when renaming Services to ApplicationServices on a few other types in #41900 (comment).

Proposed API

namespace Microsoft.AspNetCore.Builder;

public abstract class EndpointBuilder
{
-    public IServiceProvider? ServiceProvider { get; set; }
+    public IServiceProvider ApplicationServices { get; set; }
}

Usage Examples

var serviceProvider = routeEndpointBuilder.ApplicationServices;
var hostEnvironment = serviceProvider.GetService<IHostEnvironment>();
var serviceProviderIsService = serviceProvider.GetService<IServiceProviderIsService>();

Alternative Designs

Keep ApplicationServices nullable. OpenApiRouteHandlerBuilderExtensions currently has different behavior if the IServiceProvider is null or not. If it's null it doesn't add any metadata to the endpoint.

var serviceProvider = routeEndpointBuilder.ServiceProvider;
if (methodInfo == null || serviceProvider == null)
{
return null;
}
var hostEnvironment = serviceProvider.GetService<IHostEnvironment>();
var serviceProviderIsService = serviceProvider.GetService<IServiceProviderIsService>();

Is this what we want for WithOpenApi? Could it be valid to change behavior given a "null" ServiceProvider for other things adding endpoint metadata? What about route handler filters with RouteHandlerContext.ApplicationServices? Technically, we're only hiding information by making these non-nullable. Maybe this is best to hide this information so people don't short circuit for a null service provider. I'm not sure. What do we think @captainsafia @DamianEdwards @davidfowl?

We could also keep the ServiceProvider name even though we've avoided it in other APIs. It does align with IEndpointRouteBuilder.ServiceProvider that first shipped in 3.0. I do like the increased clarity of ApplicationServices though, and we seem to use that in more places.

Risks

This hasn't shipped in a major release, so the risk is not doing this before we are unable to make breaking changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions