9. Polly in .NET Core 2.1 and the HttpClientFactory Flashcards

1
Q

Before the release of HttpClientFactory, what two ways was the HttpClient used?

A
  1. Short-lived — a new HttpClient is created for every single request made.
  2. Long-lived — the same HttpClient is used for all outgoing requests to an endpoint.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the issue with the short-lived (instance per request) method of using the HttpClient?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the issue with the long-lived (single instance) method of using the HttpClient?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What problems do the HttpClientFactory solve and how?

A

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 HttpClientHandlers 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the use of HttpClientFactory mean for us in terms of the point and time we specify our policies for a given request?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Where do we do the work of defining Polly policies for the endpoint(s) when we’re using the HttpClientFactory?

A

It’s done where we define the factory, not where we’re making the requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What NuGet package do we need to add in order to be able to use Polly policies with HttpClientFactory?

A

The Microsoft.Extensions.Http.Polly package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we add a HttpClientFactory to a services (IServiceCollection) instance?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we directly add a Polly policy to a HttpClientFactory?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we create a HttpClient from a HttpClientFactory and what is returned by this?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we define multiple policies for a HttpClientFactory where we pick from them depending on the request that we’re making?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we add a policy registry to our services instance and what is returned by this?

A

By calling the AddPolicyRegistry extension method on the services instance. This returns back the policy registry (IPolicyRegistry) instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

The AddPolicyHandlerFromRegistry method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly