HttpClient One-Pager

one-pagers aspnetcore csharp

Introduction

  • The HttpClient class sends http requests and receives http responses from a Uri.

  • An HttpClient object instance is a collection of settings that is applied to all requests.

  • Each object instance uses its own connection pool, which isolates its requests from the others.

HttpClient

HttpResponseMessage

HttpRequestException

HttpProxy

  • A proxy can be configured in 2 ways.

    • A default is specified on the HttpClient.DefaultProxy property.

    • Or you can specify the HttpClientHandler.Proxy property.

  • The HttpClient.DefaultProxy is a static property that determines the default proxy that all HttpClient instances use. See Global default proxy

  • See Proxy per client

IHttpClientFactory

  • IHttpClientFactory serves as an abstraction that can create HttpClient instances with custom configurations.

  • If your app uses cookies then it is better to avoid IHttpClientFactory.

  • Use IHttpClientFactory for short-lived clients. Use PooledConnectionLifetime for long-lived clients.

  • See IHttpClientFactory

  • To regiser IHttpClientFactory, call AddHttpClient. See Basic Usage

  • Use named clients, if your app needs to connect to multiple services. See Named Clients

  • Typed clients are the same as named clients but without the use of strings. Probably better to use this. See Typed Clients

  • Get, post, put method calls are no different from HttpClient. See Get, Post, Put

HttpClient lifetime Management

  • IHttpClientFactory creates a new instance of HttpClient for every call to CreateClient.

  • One HttpClientHandler is created per client name.

  • The factory manages the lifetimes of the HttpClientHandler instances.

  • To reduce resource consumption, IHttpClientFactory caches HttpClientHandler instances.

  • An HttpClientHandler instance may be reused from the cache if its lifetime has not expired.

  • Caching of handlers is desirable as each handler manages its own underlying connection pool.

  • Creating more handlers may lead to socket exhaustion and connection delays.

  • Some handlers may also keep the connection open indefinitely, which can prevent the handler reacting to DNS changes.

  • The default handler lifetime is two minutes.

  • You can change the lifetime value. see HttpClient Lifetime Management.

SocketsHttpHandler

  • The SocketsHttpHandler implementation of HttpMessageHandler allows PooledConnectionLifetime to be configured.

  • This setting is used to ensure that the handler reacts to DNS changes, so using SocketsHttpHandler is an alternative to using IHttpClientFactory.

  • IHttpClientFactory and SocketsHttpHandler can be used together. See the details

Use named clients in singleton service.

  • With named clients, IHttpClientFactory is injected into services, a new HttpClient is created everytime CreateClient is called.

  • Typed clients are transient objects usually injected into services. Typed clients are supposed to be short-lived. Singleton services are supposed to be long-lived. See the problem there.

  • For more information

Message Handler DI Scope

  • IHttpClientFactory creates a seperate DI scope per each HttpMessageHandler instance.

  • They are separate from application DI scope like aspnetcore request scope or user-created manual DI scope.

  • HttpMessageHandler scopes can outlive application scopes.

  • Do not cache scope related information inside HttpMessageHandler instances.

  • Generally avoid such code gymnastics. Use this section more like an fyi.

  • For more information

Guidelines for using HttpClient

  • If a DNS entry changes regularly, the client wont respect those updates. Limit the lifetime of the connection by setting the PooledConnectionLifetime property, so that DNS lookup is repeated when the connection is replaced. For more information

  • To avoid port exhaustion problems, reusing HttpClient instance for as many Http requests as possible is recommended. Recommended usage

Rate limit an Http handler

Http/3 support

  • In case, you really want to use Http/3. I would not recommend using it at this point of time.

  • If you are curious