-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. Fixes #87914 --------- Co-authored-by: Natalia Kondratyeva <knatalia@microsoft.com>
- Loading branch information
1 parent
1318299
commit 7980417
Showing
8 changed files
with
406 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
src/libraries/Microsoft.Extensions.Http/src/DependencyInjection/DefaultHttpClientBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...soft.Extensions.Http/src/DependencyInjection/DefaultHttpClientBuilderServiceCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
internal sealed class DefaultHttpClientBuilderServiceCollection : IServiceCollection | ||
{ | ||
private readonly IServiceCollection _services; | ||
private readonly bool _isDefault; | ||
private readonly DefaultHttpClientConfigurationTracker _tracker; | ||
|
||
public DefaultHttpClientBuilderServiceCollection(IServiceCollection services, bool isDefault, DefaultHttpClientConfigurationTracker tracker) | ||
{ | ||
_services = services; | ||
_isDefault = isDefault; | ||
_tracker = tracker; | ||
} | ||
|
||
public void Add(ServiceDescriptor item) | ||
{ | ||
if (item.ServiceType != typeof(IConfigureOptions<HttpClientFactoryOptions>)) | ||
{ | ||
_services.Add(item); | ||
return; | ||
} | ||
|
||
if (_isDefault) | ||
{ | ||
// Insert IConfigureOptions<HttpClientFactoryOptions> services into the collection before named config descriptors. | ||
// This ensures they run and apply configuration first. Configuration for named clients run afterwards. | ||
if (_tracker.InsertDefaultsAfterDescriptor != null && | ||
_services.IndexOf(_tracker.InsertDefaultsAfterDescriptor) is var index && index != -1) | ||
{ | ||
index++; | ||
_services.Insert(index, item); | ||
} | ||
else | ||
{ | ||
_services.Add(item); | ||
} | ||
|
||
_tracker.InsertDefaultsAfterDescriptor = item; | ||
} | ||
else | ||
{ | ||
// Track the location of where the first named config descriptor was added. | ||
_tracker.InsertDefaultsAfterDescriptor ??= _services.Last(); | ||
|
||
_services.Add(item); | ||
} | ||
} | ||
|
||
public ServiceDescriptor this[int index] | ||
{ | ||
get => _services[index]; | ||
set => _services[index] = value; | ||
} | ||
public int Count => _services.Count; | ||
public bool IsReadOnly => _services.IsReadOnly; | ||
public void Clear() => _services.Clear(); | ||
public bool Contains(ServiceDescriptor item) => _services.Contains(item); | ||
public void CopyTo(ServiceDescriptor[] array, int arrayIndex) => _services.CopyTo(array, arrayIndex); | ||
public IEnumerator<ServiceDescriptor> GetEnumerator() => _services.GetEnumerator(); | ||
public int IndexOf(ServiceDescriptor item) => _services.IndexOf(item); | ||
public void Insert(int index, ServiceDescriptor item) => _services.Insert(index, item); | ||
public bool Remove(ServiceDescriptor item) => _services.Remove(item); | ||
public void RemoveAt(int index) => _services.RemoveAt(index); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...icrosoft.Extensions.Http/src/DependencyInjection/DefaultHttpClientConfigurationTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
internal sealed class DefaultHttpClientConfigurationTracker | ||
{ | ||
public ServiceDescriptor? InsertDefaultsAfterDescriptor { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.