306
CHAPTER 7 | Implement resilient applications
Reference the .NET 7 packages
IHttpClientFactory
is available since .NET Core 2.1, however, we recommend you use the latest .NET
7 packages from NuGet in your project. You typically also need to reference the extension package
Microsoft.Extensions.Http.Polly
.
Configure a client with Polly’s Retry policy, in app startup
As shown in previous sections, you need to define a named or typed client
HttpClient configuration in
your standard
Program.cs
app configuration. Now you add incremental code specifying the policy for
the Http retries with exponential backoff, as below:
// Program.cs
builder.
Services
.
AddHttpClient
()
.
SetHandlerLifetime
(TimeSpan.
FromMinutes
(
5
))
//Set lifetime to five minutes
.
AddPolicyHandler
(
GetRetryPolicy
());
The
Dostları ilə paylaş: