![]() ![]() When using Named clients, the HttpClient instance configurations can be specified while registering the service with the Dependency Injection container. While you can inject the configuration setting and other information into the Controller, the container soon starts violating the Single Responsibility Principle (SRP). in most cases that I saw on the web, what is called REST client is actually a HTTP client, since it uses HTTP only and can communicate. REST client is a client that is designed to use a service from a server and this service is RESTful. However, often when we need to make connections to external services, we also need a set of associated configuration details like URL, secret keys, special request headers, etc. HTTP client is a client that is able to send a request to and get a response from the server in HTTP format. The above usage of IHttpClientFactory is referred to as Basic usage, by directly injecting the factory instance into the Controller or class that requires an HttpClient instance. ![]() There are different ways we can use IHttpClientFactory in our application code. If you are interested in learning more about how it works internally checkout out this link here. ![]() The connections are reused and automatically disposed to avoid DNS level issues. Using the factory to create the client reuses connection from a connection pool, thereby not creating too many sockets. Using the IHttpClientFactory has several benefits, including managing the lifetime of the network connections. To enable Dependency Injection of the IHttpClientFactory instance we need to make sure to call services.AddHttpClient() method in ConfigureServices method of Startup.cs. Using ( var httpClient = new HttpClient ( ) ) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |