Skip to content

[API Proposal]: Add support for HostApplicationBuilder in AmbientMetadata extension #6509

@julealgon

Description

@julealgon

Background and motivation

The Microsoft.Extensions.AmbientMetadata has a UseApplicationMetadata that currently targets the old IHostBuilder interface:

public static IHostBuilder UseApplicationMetadata(this IHostBuilder builder, string sectionName = DefaultSectionName)

I would like to propose the creation of a version of this method that targets IHostApplicationBuilder instead, which is the more modern, property-based hosting API.

API Proposal

public static class ApplicationMetadataHostBuilderExtensions
{
    public static TBuilder UseApplicationMetadata<TBuilder>(this TBuilder builder, string sectionName = DefaultSectionName)
        where TBuilder : IHostApplicationBuilder;
}

API Usage

var builder = WebApplication.CreateBuilder(args);

builder.UseApplicationMetadata();

// ...

var app = builder.Build();

// ,,,

await app.RunAsync();

Alternative Designs

Methods that work directly on the top-level IHostApplicationBuilder are somewhat unusual, so perhaps the library should also (or instead) consider providing an easier-to-use versions of:

public static IServiceCollection AddApplicationMetadata(this IServiceCollection services, IConfigurationSection section)

So that we can simply use:

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddApplicationMetadata();

builder.Services.AddApplicationMetadata();

Instead.

Currently, the second call is not possible as the caller is asked to provide the configuration section.

Risks

I believe zero, considering these would be additive APIs to the ones that exist today, so people can opt-in to the new HostApplicationBuilder-based configuration interface.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-hosting

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions