Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class Microsoft.​Extensions.​DependencyInjection.​HttpClientBuilderExtensions

Assembly: Microsoft.Extensions.Http

Inheritance: object → HttpClientBuilderExtensions

Extension methods for configuring an <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .

Methods

public static IHttpClientBuilder
AddAsKeyed​(IHttpClientBuilder builder, ServiceLifetime lifetime = 1)
Registers a named <see cref="T:System.Net.Http.HttpClient" /> and the related handler pipeline <see cref="T:System.Net.Http.HttpMessageHandler" /> as keyed services with the client's name as the key, and a lifetime provided in the <paramref name="lifetime" /> parameter. By default, the lifetime is <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
lifetime Lifetime of the keyed services registered.
Remarks <para> A named client resolved from DI as a keyed service will behave similarly to a client you would create with <see cref="M:System.Net.Http.IHttpClientFactory.CreateClient(System.String)" />. This means that the client will continue reusing the same <see cref="T:System.Net.Http.HttpMessageHandler" /> instance for the duration of <see cref="P:Microsoft.Extensions.Http.HttpClientFactoryOptions.HandlerLifetime" />, and it will continue to use the separate, handler's DI scope instead of the scope it was resolved from. </para> <para> WARNING: Registering the client as a keyed <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient" /> service will lead to the <see cref="T:System.Net.Http.HttpClient" /> and <see cref="T:System.Net.Http.HttpMessageHandler" /> instances being captured by DI as both implement <see cref="T:System.IDisposable" />. This might lead to memory leaks if the client is resolved multiple times within a <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton" /> service. </para> <para> WARNING: In case of (1) a keyed <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton" /><see cref="T:System.Net.Http.HttpClient" /> registration, or (2) a keyed <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient" /><see cref="T:System.Net.Http.HttpClient" /> injected into a <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton" /> service, or (3) long-running application scopes, the <see cref="T:System.Net.Http.HttpClient" /> instances will get captured by a singleton or a long-running scope, so they will NOT be able to participate in the handler rotation, which can result in the loss of DNS changes. (This is a similar issue to the one with Typed Clients, that are registered as <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient" /> services.) </para> <para> If called twice with for a builder with the same name, the lifetime of the keyed service will be updated to the latest used <see cref="T:Microsoft.Extensions.DependencyInjection.ServiceLifetime" /> value. </para> <para> If called for a typed client, only the related named client and handler will be registered as keyed. The typed client itself will continue to be registered as a transient service. </para> <para> If used in conjunction with <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions.ConfigureHttpClientDefaults(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.DependencyInjection.IHttpClientBuilder})" />, the key <see cref="P:Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey" /> is used, so any named <see cref="T:System.Net.Http.HttpClient" /> instance will be resolvable as a keyed service (unless explicitly opted-out from the keyed registration via <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.RemoveAsKeyed(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" />). </para>
public static IHttpClientBuilder
AddDefaultLogger​(IHttpClientBuilder builder)
Adds back the default logging for a named <see cref="T:System.Net.Http.HttpClient" /> , if it was removed previously by calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.RemoveAllLoggers(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
public static IHttpClientBuilder
AddHttpMessageHandler​(IHttpClientBuilder builder, System.​Func<​System.​IServiceProvider, System.​Net.​Http.​DelegatingHandler> configureHandler)
Adds a delegate that will be used to create an additional message handler for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler A delegate that is used to create a <see cref="T:System.Net.Http.DelegatingHandler" /> .
Remarks <para> The <paramref name="configureHandler" /> delegate should return a new instance of the message handler each time it is invoked. </para> <para> The <see cref="T:System.IServiceProvider" /> argument provided to <paramref name="configureHandler" /> will be a reference to a scoped service provider that shares the lifetime of the handler being constructed. </para>
public static IHttpClientBuilder
AddHttpMessageHandler​(IHttpClientBuilder builder, System.​Func<​System.​Net.​Http.​DelegatingHandler> configureHandler)
Adds a delegate that will be used to create an additional message handler for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler A delegate that is used to create a <see cref="T:System.Net.Http.DelegatingHandler" /> .
Remarks The <paramref name="configureHandler" /> delegate should return a new instance of the message handler each time it is invoked.
public static IHttpClientBuilder
AddHttpMessageHandler​(IHttpClientBuilder builder)
Adds an additional message handler from the dependency injection container for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
Remarks <para> The <typeparamref name="THandler" /> will be resolved from a scoped service provider that shares the lifetime of the handler being constructed. </para>
public static IHttpClientBuilder
AddLogger​(IHttpClientBuilder builder, System.​Func<​System.​IServiceProvider, Http.​Logging.​IHttpClientLogger> httpClientLoggerFactory, bool wrapHandlersPipeline = false)
Adds a delegate that will be used to create an additional logger for a named <see cref="T:System.Net.Http.HttpClient" /> . The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
httpClientLoggerFactory A delegate that is used to create a custom logger. The logger should implement <see cref="T:Microsoft.Extensions.Http.Logging.IHttpClientLogger" /> or <see cref="T:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger" /> .
wrapHandlersPipeline Whether the logging handler with the custom logger would be added to the top or to the bottom of the additional handlers chains.
Remarks <para> If the <paramref name="wrapHandlersPipeline" /> is `true`, <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> would be executed before all other additional handlers in the chain. <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStop(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStopAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan,System.Threading.CancellationToken)" /> would be executed after all other additional handlers, essentially wrapping the whole pipeline. </para> <para> If the <paramref name="wrapHandlersPipeline" /> is `false`, <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> would be executed after all other additional handlers in the chain, right before the primary handler. <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStop(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStopAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan,System.Threading.CancellationToken)" /> would be executed before all other additional handlers, right after the primary handler. </para> <para> The <see cref="T:System.IServiceProvider" /> argument provided to <paramref name="httpClientLoggerFactory" /> will be a reference to a scoped service provider that shares the lifetime of the handler chain being constructed. </para> <para> If <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddLogger(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.IServiceProvider,Microsoft.Extensions.Http.Logging.IHttpClientLogger},System.Boolean)" /> is called multiple times, multiple loggers would be added. If <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.RemoveAllLoggers(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" /> was not called before calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddLogger(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.IServiceProvider,Microsoft.Extensions.Http.Logging.IHttpClientLogger},System.Boolean)" />, then new logger would be added in addition to the default ones. </para>
public static IHttpClientBuilder
AddLogger​(IHttpClientBuilder builder, bool wrapHandlersPipeline = false)
Adds a delegate that will be used to create an additional logger for a named <see cref="T:System.Net.Http.HttpClient" /> . The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
wrapHandlersPipeline Whether the logging handler with the custom logger would be added to the top or to the bottom of the additional handlers chains.
Remarks <para> If the <paramref name="wrapHandlersPipeline" /> is `true`, <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> would be executed before all other additional handlers in the chain. <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStop(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStopAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan,System.Threading.CancellationToken)" /> would be executed after all other additional handlers, essentially wrapping the whole pipeline. </para> <para> If the <paramref name="wrapHandlersPipeline" /> is `false`, <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> would be executed after all other additional handlers in the chain, right before the primary handler. <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStop(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan)" /> and <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStopAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan,System.Threading.CancellationToken)" /> would be executed before all other additional handlers, right after the primary handler. </para> <para> The <typeparamref name="TLogger" /> will be resolved from a scoped service provider that shares the lifetime of the handler chain being constructed. </para> <para> If <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddLogger``1(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Boolean)" /> is called multiple times, multiple loggers would be added. If <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.RemoveAllLoggers(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" /> was not called before calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddLogger``1(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Boolean)" />, then new logger would be added in addition to the default ones. </para>
public static IHttpClientBuilder
AddTypedClient​(IHttpClientBuilder builder)
Configures a binding between the <typeparamref name="TClient" /> type and the named <see cref="T:System.Net.Http.HttpClient" /> associated with the <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
Remarks <para> <typeparamref name="TClient" /> instances constructed with the appropriate <see cref="T:System.Net.Http.HttpClient" /> can be retrieved from <see cref="M:System.IServiceProvider.GetService(System.Type)" /> (and related methods) by providing <typeparamref name="TClient" /> as the service type. </para> <para> Calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTypedClient``1(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" /> will register a typed client binding that creates <typeparamref name="TClient" /> using the <see cref="T:Microsoft.Extensions.Http.ITypedHttpClientFactory`1" />. </para> <para> The typed client's service dependencies will be resolved from the same service provider that is used to resolve the typed client. It is not possible to access services from the scope bound to the message handler, which is managed independently. </para>
public static IHttpClientBuilder
AddTypedClient​(IHttpClientBuilder builder, System.​Func<​System.​Net.​Http.​HttpClient, System.​IServiceProvider, TClient> factory)
Configures a binding between the <typeparamref name="TClient" /> type and the named <see cref="T:System.Net.Http.HttpClient" /> associated with the <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
factory A factory function that will be used to construct the typed client.
Remarks <para> <typeparamref name="TClient" /> instances constructed with the appropriate <see cref="T:System.Net.Http.HttpClient" /> can be retrieved from <see cref="M:System.IServiceProvider.GetService(System.Type)" /> (and related methods) by providing <typeparamref name="TClient" /> as the service type. </para> <para> Calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTypedClient``1(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.Net.Http.HttpClient,System.IServiceProvider,``0})" /> will register a typed client binding that creates <typeparamref name="TClient" /> using the provided factory function. </para>
public static IHttpClientBuilder
AddTypedClient​(IHttpClientBuilder builder, System.​Func<​System.​Net.​Http.​HttpClient, TClient> factory)
Configures a binding between the <typeparamref name="TClient" /> type and the named <see cref="T:System.Net.Http.HttpClient" /> associated with the <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
factory A factory function that will be used to construct the typed client.
Remarks <para> <typeparamref name="TClient" /> instances constructed with the appropriate <see cref="T:System.Net.Http.HttpClient" /> can be retrieved from <see cref="M:System.IServiceProvider.GetService(System.Type)" /> (and related methods) by providing <typeparamref name="TClient" /> as the service type. </para> <para> Calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTypedClient``1(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.Net.Http.HttpClient,``0})" /> will register a typed client binding that creates <typeparamref name="TClient" /> using the provided factory function. </para>
public static IHttpClientBuilder
AddTypedClient​(IHttpClientBuilder builder)
Configures a binding between the <typeparamref name="TClient" /> type and the named <see cref="T:System.Net.Http.HttpClient" /> associated with the <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> . The created instances will be of type <typeparamref name="TImplementation" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
Remarks <para> <typeparamref name="TClient" /> instances constructed with the appropriate <see cref="T:System.Net.Http.HttpClient" /> can be retrieved from <see cref="M:System.IServiceProvider.GetService(System.Type)" /> (and related methods) by providing <typeparamref name="TClient" /> as the service type. </para> <para> Calling <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTypedClient``2(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder)" /> will register a typed client binding that creates <typeparamref name="TImplementation" /> using the <see cref="T:Microsoft.Extensions.Http.ITypedHttpClientFactory`1" />. </para> <para> The typed client's service dependencies will be resolved from the same service provider that is used to resolve the typed client. It is not possible to access services from the scope bound to the message handler, which is managed independently. </para>
public static IHttpClientBuilder
ConfigureAdditionalHttpMessageHandlers​(IHttpClientBuilder builder, System.​Action<​System.​Collections.​Generic.​IList<​System.​Net.​Http.​DelegatingHandler>, System.​IServiceProvider> configureAdditionalHandlers)
Adds a delegate that will be used to configure additional message handlers using <see cref="T:Microsoft.Extensions.Http.HttpMessageHandlerBuilder" /> for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureAdditionalHandlers A delegate that is used to configure a collection of <see cref="T:System.Net.Http.DelegatingHandler" /> s.
public static IHttpClientBuilder
ConfigureHttpClient​(IHttpClientBuilder builder, System.​Action<​System.​IServiceProvider, System.​Net.​Http.​HttpClient> configureClient)
Adds a delegate that will be used to configure a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureClient A delegate that is used to configure an <see cref="T:System.Net.Http.HttpClient" /> .
Remarks The <see cref="T:System.IServiceProvider" /> provided to <paramref name="configureClient" /> will be the same application's root service provider instance.
public static IHttpClientBuilder
ConfigureHttpClient​(IHttpClientBuilder builder, System.​Action<​System.​Net.​Http.​HttpClient> configureClient)
Adds a delegate that will be used to configure a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureClient A delegate that is used to configure an <see cref="T:System.Net.Http.HttpClient" /> .
public static IHttpClientBuilder
ConfigureHttpMessageHandlerBuilder​(IHttpClientBuilder builder, System.​Action<​Http.​HttpMessageHandlerBuilder> configureBuilder)
Adds a delegate that will be used to configure message handlers using <see cref="T:Microsoft.Extensions.Http.HttpMessageHandlerBuilder" /> for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureBuilder A delegate that is used to configure an <see cref="T:Microsoft.Extensions.Http.HttpMessageHandlerBuilder" /> .
public static IHttpClientBuilder
ConfigurePrimaryHttpMessageHandler​(IHttpClientBuilder builder, System.​Func<​System.​IServiceProvider, System.​Net.​Http.​HttpMessageHandler> configureHandler)
Adds a delegate that will be used to configure the primary <see cref="T:System.Net.Http.HttpMessageHandler" /> for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler A delegate that is used to create an <see cref="T:System.Net.Http.HttpMessageHandler" /> .
Remarks <para> The <paramref name="configureHandler" /> delegate should return a new instance of the message handler each time it is invoked. </para> <para> The <see cref="T:System.IServiceProvider" /> argument provided to <paramref name="configureHandler" /> will be a reference to a scoped service provider that shares the lifetime of the handler being constructed. </para>
public static IHttpClientBuilder
ConfigurePrimaryHttpMessageHandler​(IHttpClientBuilder builder, System.​Func<​System.​Net.​Http.​HttpMessageHandler> configureHandler)
Adds a delegate that will be used to configure the primary <see cref="T:System.Net.Http.HttpMessageHandler" /> for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler A delegate that is used to create an <see cref="T:System.Net.Http.HttpMessageHandler" /> .
Remarks The <paramref name="configureHandler" /> delegate should return a new instance of the message handler each time it is invoked.
public static IHttpClientBuilder
ConfigurePrimaryHttpMessageHandler​(IHttpClientBuilder builder)
Configures the primary <see cref="T:System.Net.Http.HttpMessageHandler" /> from the dependency injection container for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
Remarks <para> The <typeparamref name="THandler" /> will be resolved from a scoped service provider that shares the lifetime of the handler being constructed. </para>
public static IHttpClientBuilder
ConfigurePrimaryHttpMessageHandler​(IHttpClientBuilder builder, System.​Action<​System.​Net.​Http.​HttpMessageHandler, System.​IServiceProvider> configureHandler)
Adds a delegate that will be used to configure the primary <see cref="T:System.Net.Http.HttpMessageHandler" /> for a named <see cref="T:System.Net.Http.HttpClient" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler A delegate that is used to configure a previously set or default primary <see cref="T:System.Net.Http.HttpMessageHandler" /> .
Remarks <para> The <see cref="T:System.IServiceProvider" /> argument provided to <paramref name="configureHandler" /> will be a reference to a scoped service provider that shares the lifetime of the handler being constructed. </para>
public static IHttpClientBuilder
RedactLoggedHeaders​(IHttpClientBuilder builder, System.​Collections.​Generic.​IEnumerable<​string> redactedLoggedHeaderNames)
Sets the collection of HTTP headers names for which values should be redacted before logging.
Returns The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
redactedLoggedHeaderNames The collection of HTTP headers names for which values should be redacted before logging.
public static IHttpClientBuilder
RedactLoggedHeaders​(IHttpClientBuilder builder, System.​Func<​string, bool> shouldRedactHeaderValue)
Sets the <see cref="T:System.Func`2" /> which determines whether to redact the HTTP header value given its corresponding header name before logging.
Returns The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
shouldRedactHeaderValue The <see cref="T:System.Func`2" /> which determines whether to redact the HTTP header value given its corresponding header name before logging.
Remarks The provided <paramref name="shouldRedactHeaderValue" /> predicate will be evaluated for each header name when logging. If the predicate returns <see langword="true" /> then the header value will be replaced with a marker value <c>*</c> in logs; otherwise the header value will be logged.
public static IHttpClientBuilder
RemoveAllLoggers​(IHttpClientBuilder builder)
Removes all previously added loggers for a named <see cref="T:System.Net.Http.HttpClient" /> , including default ones.
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
public static IHttpClientBuilder
RemoveAsKeyed​(IHttpClientBuilder builder)
Removes the keyed registrations for the named <see cref="T:System.Net.Http.HttpClient" /> and <see cref="T:System.Net.Http.HttpMessageHandler" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
Remarks <para> If used in conjunction with <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions.ConfigureHttpClientDefaults(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.DependencyInjection.IHttpClientBuilder})" />, it will only affect the previous "global" <see cref="P:Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey" /> registration, and won't affect the clients registered for a specific name with <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddAsKeyed(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,Microsoft.Extensions.DependencyInjection.ServiceLifetime)" />. </para>
public static IHttpClientBuilder
SetHandlerLifetime​(IHttpClientBuilder builder, System.​TimeSpan handlerLifetime)
Sets the length of time that a <see cref="T:System.Net.Http.HttpMessageHandler" /> instance can be reused. Each named client can have its own configured handler lifetime value. The default value is two minutes. Set the lifetime to <see cref="F:System.Threading.Timeout.InfiniteTimeSpan" /> to disable handler expiry.
Remarks <para> The default implementation of <see cref="T:System.Net.Http.IHttpClientFactory" /> will pool the <see cref="T:System.Net.Http.HttpMessageHandler" /> instances created by the factory to reduce resource consumption. This setting configures the amount of time a handler can be pooled before it is scheduled for removal from the pool and disposal. </para> <para> Pooling of handlers is desirable as each handler typically manages its own underlying HTTP connections; creating more handlers than necessary can result in connection delays. Some handlers also keep connections open indefinitely which can prevent the handler from reacting to DNS changes. The value of <paramref name="handlerLifetime" /> should be chosen with an understanding of the application's requirement to respond to changes in the network environment. </para> <para> Expiry of a handler will not immediately dispose the handler. An expired handler is placed in a separate pool which is processed at intervals to dispose handlers only when they become unreachable. Using long-lived <see cref="T:System.Net.Http.HttpClient" /> instances will prevent the underlying <see cref="T:System.Net.Http.HttpMessageHandler" /> from being disposed until all references are garbage-collected. </para>
public static IHttpClientBuilder
UseSocketsHttpHandler​(IHttpClientBuilder builder, System.​Action?<​System.​Net.​Http.​SocketsHttpHandler, System.​IServiceProvider> configureHandler = null)
Adds or updates <see cref="T:System.Net.Http.SocketsHttpHandler" /> as a primary handler for a named <see cref="T:System.Net.Http.HttpClient" /> . If provided, also adds a delegate that will be used to configure the primary <see cref="T:System.Net.Http.SocketsHttpHandler" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureHandler Optional delegate that is used to configure the primary <see cref="T:System.Net.Http.SocketsHttpHandler" /> .
Remarks <para> If a primary handler was already set to be <see cref="T:System.Net.Http.SocketsHttpHandler" /> by previously calling, for example, <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.ConfigurePrimaryHttpMessageHandler(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.Net.Http.HttpMessageHandler})" /> or <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.UseSocketsHttpHandler(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Action{Microsoft.Extensions.DependencyInjection.ISocketsHttpHandlerBuilder})" />, then the passed <paramref name="configureHandler" /> delegate will be applied to the existing instance. Otherwise, a new instance of <see cref="T:System.Net.Http.SocketsHttpHandler" /> will be created. </para>
public static IHttpClientBuilder
UseSocketsHttpHandler​(IHttpClientBuilder builder, System.​Action<​ISocketsHttpHandlerBuilder> configureBuilder)
Adds or updates <see cref="T:System.Net.Http.SocketsHttpHandler" /> as a primary handler for a named <see cref="T:System.Net.Http.HttpClient" /> and configures it using <see cref="T:Microsoft.Extensions.DependencyInjection.ISocketsHttpHandlerBuilder" /> .
Returns An <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> that can be used to configure the client.
builder The <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" /> .
configureBuilder Delegate that is used to set up the configuration of the primary <see cref="T:System.Net.Http.SocketsHttpHandler" /> on <see cref="T:Microsoft.Extensions.DependencyInjection.ISocketsHttpHandlerBuilder" /> that will later be applied on the primary handler during its creation.
Remarks <para> If a primary handler was already set to be <see cref="T:System.Net.Http.SocketsHttpHandler" /> by previously calling, for example, <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.ConfigurePrimaryHttpMessageHandler(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Func{System.Net.Http.HttpMessageHandler})" /> or <see cref="M:Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.UseSocketsHttpHandler(Microsoft.Extensions.DependencyInjection.IHttpClientBuilder,System.Action{Microsoft.Extensions.DependencyInjection.ISocketsHttpHandlerBuilder})" />, then the configuration set on <see cref="T:Microsoft.Extensions.DependencyInjection.ISocketsHttpHandlerBuilder" /> will be applied to the existing instance. Otherwise, a new instance of <see cref="T:System.Net.Http.SocketsHttpHandler" /> will be created. </para>
public bool
Equals​(object obj)
Inherited from object
protected void
Finalize​()
Inherited from object
public int
GetHashCode​()
Inherited from object
GetType​()
Inherited from object
protected object
MemberwiseClone​()
Inherited from object
public string
ToString​()
Inherited from object