Take advantage of HttpClientFactory to manage HttpClient instances efficiently Credit: Thinkstock ASP.Net Core is an open source, cross-platform, lean, and modular framework for building high-performance web applications, introduced in ASP.Net Core 2.1. This article explains HttpClientFactory and how you can work with it in ASP.Net Core. What is HttpClientFactory and why you should use it You can take advantage of HttpClientFactory to preconfigure named HttpClient instances. Note that the HttpClientFactory is a central location that can be used to register, configure, and consume HttpClient instances that your application might need. (Incidentally, the HttpClient was introduced in .Net Framework 4.5 and is the most popular way to consume HTTP requests in .Net.) Creating too many HttpClient instances is inefficient because your application would need to bear the cost of reconnecting to the remote server whenever a new client needs to connect to it. Another problem in creating multiple HttpClient instances—tat is, creating a new HttpClient instance for every request that your application needs to process—is that the available sockets might get exhausted when there is heavy traffic. The recommended practice is to create a single shared HttpClient instance so the connections can be reused. However, even if you use a single shared HttpClient instance, there are problems galore. One is that the connections are kept alive and so will not respect the DNS Time to Live (TTL) settings. HttpClientFactory solves the problems discussed above. The HttpClientFactory is designed to manage HttpClient instances efficiently; it can manage the lifetime of the HttpClientHandler instances. Getting started using HttpClientFactory in ASP.Net Core Getting started with HttpClientFactory is easy. To work with an application in Visual Studio that targets .Net Core 2.1, you should have Visual Studio 2017 15.7 or later installed in your system. Although you can use .Net Core 2.1 in Visual Studio 20017 15.6, there are some known issues. Assuming that you’re running Visual Studio 2017 and that .Net Core 2.1 is already installed in your system, follow these steps to create an ASP.Net Core project in Visual Studio 2017: In the Visual Studio IDE, choose File > New > Project. Choose ASP.Net Core Web Application (.Net Core) from the list of the templates displayed. Specify a name for the project. Click OK to save the project. Select API in the New .Net Core Web Application window. Choose ASP.Net Core 2.1 from the dropdown list at the top. Uncheck the Enable Docker Support checkbox. Select No Authentication because you won’t be using authentication in this example. Click OK. This creates a new ASP.Net Core 2.1 Project in Visual Studio 2017. Now, head over to the Startup.cs file and start using HttpClientFactory as described in the next section. Using the HttpClientFactory There are two ways in which you can use HttpClientFactory: NamedClient (this is the default) and TypedClient. The following code snippet illustrates how you can use the HttpClientFactory as a NamedClient. Note how the AddHttpClient method has been used in the ConfigureServices method. public void ConfigureServices(IServiceCollection services) { services.AddHttpClient("IDGCustomApi", client => { client.BaseAddress = new Uri("https://localhost:6045/"); client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("User-Agent", "IDG"); }); services.AddMvc().SetCompatibilityVersion (CompatibilityVersion.Version_2_1); } You can also use HttpClientFactory as a TypedClient. In this case, you can define custom classes and then take advantage of dependency injection to inject HttpClient. Basically, you can register a custom typed client as shown in the code snippet below. services.AddHttpClient<YourCustomTypedClient>(client => client.BaseAddress = new Uri(Configuration["YourCustomTypedClientServiceUri"])); The following code snippet illustrates a custom typed client. Note how the HttpClient instance has been injected via the constructor. public class CustomHttpClient { public HttpClient Client { get; } public CustomHttpClient(HttpClient client) { Client = client; } } You can register this custom client in the ConfigureServices method as shown in the code snippet below. Note how the custom type has been passed as a generic argument to the AddHttpClient method. public void ConfigureServices(IServiceCollection services) { services.AddHttpClient<CustomHttpClient>(client => { client.BaseAddress = new Uri("https://localhost:6045/"); client.DefaultRequestHeaders.Add("Accept", "application/json"); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos