HttpClient in .NetCore —— IHttpClientFactory & HttpMessageHandler & Header propagation middleware & Cookies & Logging
Make HTTP requests using IHttpClientFactory in ASP.NET Core | Microsoft Docs
Logging
Clients created via IHttpClientFactory
record log messages for all requests.
Enable the appropriate information level in the logging configuration to see the default log messages.
Additional logging, such as the logging of request headers, is only included at trace level.
The log category used for each client includes the name of the client. A client named MyNamedClient, for example, logs messages with a category of "System.Net.Http.HttpClient.MyNamedClient.LogicalHandler". Messages suffixed with LogicalHandler occur outside the request handler pipeline.
On the request, messages are logged before any other handlers in the pipeline have processed it.
On the response, messages are logged after any other pipeline handlers have received the response.
Logging also occurs inside the request handler pipeline. In the MyNamedClient example, those messages are logged with the log category "System.Net.Http.HttpClient.MyNamedClient.ClientHandler".
For the request, this occurs after all other handlers have run and immediately before the request is sent.
On the response, this logging includes the state of the response before it passes back through the handler pipeline.
Enabling logging outside and inside the pipeline enables inspection of the changes made by the other pipeline handlers. This may include changes to request headers or to the response status code.
Including the name of the client in the log category enables log filtering for specific named clients.
Configure the HttpMessageHandler
It may be necessary to control the configuration of the inner HttpMessageHandler
used by a client.
An IHttpClientBuilder
is returned when adding named or typed clients. The ConfigurePrimaryHttpMessageHandler extension method can be used to define a delegate. The delegate is used to create and configure the primary HttpMessageHandler
used by that client:
builder.Services.AddHttpClient("ConfiguredHttpMessageHandler") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = true, UseDefaultCredentials = true });
Cookies
The pooled HttpMessageHandler
instances results in CookieContainer
objects being shared. Unanticipated CookieContainer
object sharing often results in incorrect code. For apps that require cookies, consider either:
- Disabling automatic cookie handling
- Avoiding
IHttpClientFactory
Call ConfigurePrimaryHttpMessageHandler to disable automatic cookie handling:
builder.Services.AddHttpClient("NoAutomaticCookies") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { UseCookies = false });
Use IHttpClientFactory in a console app
In a console app, add the following package references to the project:
In the following example:
- IHttpClientFactory and
GitHubService
are registered in the Generic Host's service container. GitHubService
is requested from DI, which in-turn requests an instance ofIHttpClientFactory
.GitHubService
usesIHttpClientFactory
to create an instance ofHttpClient
, which it uses to retrieve docs GitHub branches.
Header propagation middleware
Header propagation is an ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HttpClient
requests. To use header propagation:
-
Install the Microsoft.AspNetCore.HeaderPropagation package.
-
Configure the
HttpClient
and middleware pipeline inProgram.cs
:
-
Make outbound requests using the configured
HttpClient
instance, which includes the added headers.
Additional resources
- View or download sample code (how to download)
- Use HttpClientFactory to implement resilient HTTP requests
- Implement HTTP call retries with exponential backoff with HttpClientFactory and Polly policies
- Implement the Circuit Breaker pattern
- How to serialize and deserialize JSON in .NET