9. Polly in .NET Core 2.1 and the HttpClientFactory Flashcards
Before the release of HttpClientFactory
, what two ways was the HttpClient
used?
- Short-lived — a new
HttpClient
is created for every single request made. - Long-lived — the same
HttpClient
is used for all outgoing requests to an endpoint.
What is the issue with the short-lived (instance per request) method of using the HttpClient
?
When you create a new HttpClient
for every request, you incur the overhead of the instantiation, but this is not even the main issue.
Each HttpClient
will hold open the socket that it uses for some time after the request is complete. If we are making a high volume of requests to remote systems, we can find ourselves facing socket exhaustion where a new HttpClient
cannot acquire a socket to make a request.
What is the issue with the long-lived (single instance) method of using the HttpClient
?
In this approach where we hold onto a single instance of HttpClient
for a given endpoint, we will never know if the IP address of the endpoint we are connecting to has changed because the HttpClient
holds on to the IP address it got when it first looked up the hostname.
What problems do the HttpClientFactory
solve and how?
It solves both the “socket exhaustion” issue of the short-lived and the “DNS out of date” issue of the long-lived way.
It does this by using the HttpClient
by pooling the HttpClientHandler
(which does most of the work of HttpClient
) and by disposing of HttpClientHandler
s after a specified period.
When a new HttpClientHandler
is created for an endpoint, a DNS lookup is performed. Now, we won’t wear out the sockets, and we will get a new IP address for each endpoint.
What does the use of HttpClientFactory
mean for us in terms of the point and time we specify our policies for a given request?
HttpClientFactory
lets us add Polly policies directly into it. When the request is made with the HttpClient
we get from the factory, it will automatically be executed within the Polly policy.
The lack of Polly-specific code where we’re making our requests means that we can easily retrofit Polly into an application that does not have Polly.
Where do we do the work of defining Polly policies for the endpoint(s) when we’re using the HttpClientFactory
?
It’s done where we define the factory, not where we’re making the requests.
What NuGet package do we need to add in order to be able to use Polly policies with HttpClientFactory
?
The Microsoft.Extensions.Http.Polly
package.
How do we add a HttpClientFactory
to a services (IServiceCollection) instance?
By using the services object’s AddHttpClient
extension method and passing it two parameters — the name of the client factory (string) and an Action
that configures the HttpClient
.
How do we directly add a Polly policy to a HttpClientFactory
?
Having defined the factory and added it to the services object using the AddHttpClient
method, we can then dot into this once more to call the AddPolicyHandler
method and pass it the policy to be used for the HttpClientFactory
.
How do we create a HttpClient
from a HttpClientFactory
and what is returned by this?
By requesting the client by its name using the .CreateClient
method of the factory. The name of the client is the first parameter we pass when defining it in the factory. This returns back a IHttpClientBuilder
instance.
How do we define multiple policies for a HttpClientFactory
where we pick from them depending on the request that we’re making?
We can add a policy registry with multiple policies to our services
collection and use a Func
to pick the correct policy to use for each request. In this way, we can have a different policy for each of our HTTP verbs or even for certain endpoints based on URL.
How do we add a policy registry to our services instance and what is returned by this?
By calling the AddPolicyRegistry
extension method on the services instance. This returns back the policy registry (IPolicyRegistry) instance.
What method do we use on the IHttpClientBuilder
to use a policy registry and a policy selector function to define the policy to be used for each HttpClient
?
The AddPolicyHandlerFromRegistry
method.