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 AddHttpClientDefaults #87953

Merged

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Jun 23, 2023

Fixes #87914

The AddHttpClientDefaults method supports adding configuration to all created HttpClients.

The method:

  • Creates a builder with a null name. Microsoft.Extensions.Configuration automatically applies configuration with a null name to all named configuration.
  • Ensures that default configuration is added before named configuration in the IServiceCollection. This is to make it so the order of AddHttpClientDefaults and AddHttpClient doesn't matter. Default config is always applied first, then named config is applied after. This is done by wrapping the IServiceCollection in an implementation that modifies the order that IConfigureOptions<HttpClientFactoryOptions> values are added.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Jun 23, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #87914

The AddHttpClientDefaults method supports adding configuration to all created HttpClients.

The method:

  • Creates a builder with a null name. Microsoft.Extensions.Configuration automatically applies configuration with a null name to all named configuration.
  • Ensures that default configuration is added before named configuration in the IServiceCollection. This is to make it so the order of AddHttpClientDefaults and AddHttpClient doesn't matter. Default config is always applied first, then named config is applied after. This is done by wrapping the IServiceCollection in an implementation that modifies the order that IConfigureOptions<HttpClientFactoryOptions> values are added.
Author: JamesNK
Assignees: -
Labels:

area-Extensions-HttpClientFactory

Milestone: -

@davidfowl
Copy link
Member

Ensures that default configuration is added before named configuration in the IServiceCollection. This is to make it so the order of AddHttpClientDefaults and AddHttpClient doesn't matter. Default config is always applied first, then named config is applied after. This is done by wrapping the IServiceCollection in an implementation that modifies the order that IConfigureOptions values are added.

This feels pretty bad.

@JamesNK
Copy link
Member Author

JamesNK commented Jun 23, 2023

@noahfalk @joperezr @geeknoid @davidfowl

The AddHttpClientDefaults is designed to make it easy to apply configuration to all clients configured with AddHttpClient using one extension method on IHttpClientBuilder.

The goal is to avoid having two methods:

  • Add config to one builder - extension method on IHttpClientBuilder.
  • Add config to all builders - extension method on IServiceCollection.

For example, methods in dotnet/extensions below will no longer be needed:

https://github.com/dotnet/extensions/blob/52ebc1b123613254f9dfe250515027c1332fba35/src/Libraries/Microsoft.Extensions.Http.Telemetry/Logging/HttpClientLoggingExtensions.cs#L31-L122

It will be possible to do:

// Add HttpClientLogging to all clients.
services.AddHttpClientDefaults().AddHttpClientLogging();

// Is configured with HttpClientLogging from the defaults.
services.AddHttpClient();

@davidfowl
Copy link
Member

davidfowl commented Jun 23, 2023

I've also been thinking about an API like this and was hoping we could add an overload to AddHttpClient so it wasnt 2 different calls.

services.AddHttpClient(IHttpClientBuilder clientBuilder =>
{
     clientBuilder.AddHttpMessageHandler<MyAuthHandler>();
});  

@JamesNK
Copy link
Member Author

JamesNK commented Jun 23, 2023

Ensures that default configuration is added before named configuration in the IServiceCollection. This is to make it so the order of AddHttpClientDefaults and AddHttpClient doesn't matter. Default config is always applied first, then named config is applied after. This is done by wrapping the IServiceCollection in an implementation that modifies the order that IConfigureOptions values are added.

This feels pretty bad.

It's not the best, but it works well. It hits these important requirements:

  • AddHttpClientDefaults config is overridden by named client configuration.
  • The order AddHttpClientDefaults and AddHttpClient are called doesn't matter. Named client configuration always wins.
  • It automatically works for people who have written their own IHttpClientBuilder extension methods that call services.Configure<HttpClientFactoryOptions>(...).

The extra logic is only applied to IOptionsConfiguration<HttpClientFactoryOptions> to ensure they have the right order. It won't interfere with or break other DI extension methods.

@geeknoid
Copy link
Member

@dpk83 FYI

@dpk83
Copy link

dpk83 commented Jun 23, 2023

@xakep139

@stephentoub
Copy link
Member

Let's please agree on the API via discussion on the issue and API review before opening PRs. I spoke with @CarnaViire at length about this and other related APIs yesterday and I know she was going to be following up with you.

@JamesNK JamesNK marked this pull request as draft June 23, 2023 11:49
@CarnaViire CarnaViire marked this pull request as ready for review July 12, 2023 21:39
Copy link
Member

@CarnaViire CarnaViire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for @JamesNK 's changes

@CarnaViire
Copy link
Member

@dotnet/ncl can someone pls review as well? Since I've taken over the PR.

@CarnaViire CarnaViire requested a review from a team July 12, 2023 21:56
@CarnaViire CarnaViire self-assigned this Jul 12, 2023
Copy link
Member

@ManickaP ManickaP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CarnaViire changes LGTM.

@lewing
Copy link
Member

lewing commented Jul 17, 2023

This change appears to be breaking flow into aspnetcore in dotnet/aspnetcore#49433

@joperezr
Copy link
Member

@CarnaViire can you please take a look?

@CarnaViire
Copy link
Member

I've pushed the fix to the branch dotnet/aspnetcore#49433

@ghost ghost locked as resolved and limited conversation to collaborators Aug 17, 2023
@davidfowl
Copy link
Member

So we have a problem here. I tried to use ConfigureHttpClientDefaults and noticed it isn't an extension method in the reference assembly.

@joperezr
Copy link
Member

Can you elaborate? This change seems to suggest that the reference assembly was edited and that the extension was properly added: https://github.com/dotnet/runtime/pull/87953/files#diff-06e422c9663ed696b723dc56832cbdb53b9f9c3e6d0a98e52a1cb081b7c9dc7aR50

@karelz
Copy link
Member

karelz commented Aug 22, 2023

@joperezr the this is missing -- see #90911 which David filed and which was just fixed :)

@joperezr
Copy link
Member

Oh, completely missed it! Good catch @davidfowl and thanks for the quick fix 😃

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal] ConfigureHttpClientDefaults for HttpClientFactory