Microsoft.Extensions.Http 8.0.1
About
Microsoft.Extensions.Http package provides AddHttpClient extension methods for IServiceCollection, IHttpClientFactory interface and its default implementation. This provides the ability to set up named HttpClient configurations in a DI container and later retrieve them via an injected IHttpClientFactory instance.
Key Features
- The package allows to fluently set up multiple
HttpClientconfigurations for applications that use DI viaAddHttpClientextension method. HttpClientFactorycachesHttpMessageHandlerinstances per configuration name, which allows to reuse resources betweenHttpClientinstances to avoid port exhaustion.HttpClientFactorymanages lifetime ofHttpMessageHandlerinstances and recycles connections to track DNS changes.
How to Use
Note that lifetime management of HttpClient instances created by HttpClientFactory is completely different from instances created manually. The strategies are to use either short-lived clients created by HttpClientFactory or long-lived clients with PooledConnectionLifetime set up. For more information, see the HttpClient lifetime management section in the conceptual docs and Guidelines for using HTTP clients.
Configuring HttpClient
builder.Services.AddHttpClient("foo"); // adding an HttpClient named "foo" with a default configuration
builder.Services.AddHttpClient("example", c => c.BaseAddress = new Uri("https://www.example.com")) // configuring HttpClient itself
.AddHttpMessageHandler<MyAuthHandler>() // adding additional delegating handlers to form a message handler chain
.ConfigurePrimaryHttpMessageHandler(b => new HttpClientHandler() { AllowAutoRedirect = false }) // configuring primary handler
.SetHandlerLifetime(TimeSpan.FromMinutes(30)); // changing the handler recycling interval
Using the configured HttpClient
public class MyService
{
public MyService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory; // injecting the factory
}
private Task<string> GetExampleAsync(Uri uri, CancellationToken ct)
{
HttpClient exampleClient = _httpClientFactory.CreateClient("example"); // creating the client for the specified name
return exampleClient.GetStringAsync(uri, ct); // using the client
}
}
Main Types
The main types provided by this library are:
IHttpClientFactoryIHttpMessageHandlerFactoryHttpClientFactoryServiceCollectionExtensions
Additional Documentation
- Conceptual documentation
- Also see HttpClient guidelines conceptual doc
- API documentation
- Also see
AddHttpClientextension method API doc
- Also see
Related Packages
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Http.Polly
- Microsoft.Extensions.Http.Telemetry
Feedback & Contributing
Microsoft.Extensions.Http is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
Showing the top 20 packages that depend on Microsoft.Extensions.Http.
| Packages | Downloads |
|---|---|
|
Dapr.Client
This package contains the reference assemblies for developing services using Dapr.
|
17 |
|
Dapr.AspNetCore
This package contains the reference assemblies for developing services using Dapr and AspNetCore.
|
17 |
|
Betalgo.Ranul.OpenAI
.NET library for the OpenAI services by Betalgo Ranul
|
14 |
|
Dapr.Common
Package Description
|
13 |
|
Betalgo.OpenAI
Dotnet SDK for OpenAI ChatGPT, Whisper, GPT-4 and DALL·E
|
12 |
|
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
12 |
|
Dapr.Common
Package Description
|
12 |
|
Betalgo.OpenAI
Dotnet SDK for OpenAI ChatGPT, Whisper, GPT-4 and DALL·E
|
10 |
|
Elsa.Activities.Http
Elsa is a set of workflow libraries and tools that enable lean and mean workflowing capabilities in any .NET Core application.
This package provides the following Console activities:
* ReceiveHttpRequest
* SendHttpRequest
* WriteHttpResponse
|
10 |
|
Betalgo.Ranul.OpenAI
.NET library for the OpenAI services by Betalgo Ranul
|
10 |
|
Betalgo.OpenAI
Dotnet SDK for OpenAI ChatGPT, Whisper, GPT-4 and DALL·E
|
9 |
|
Elsa.Activities.Http
Elsa is a set of workflow libraries and tools that enable lean and mean workflowing capabilities in any .NET Core application.
This package provides the following Console activities:
* ReceiveHttpRequest
* SendHttpRequest
* WriteHttpResponse
|
9 |
.NET Framework 4.6.2
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
.NET Standard 2.0
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
.NET 8.0
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
.NET 7.0
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
.NET 6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)