Edition 0 Updated to asp. Net core 0


Using HttpClientFactory in .NET



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə261/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   257   258   259   260   261   262   263   264   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

Using HttpClientFactory in .NET
https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory
 

Using HttpClientFactory in ASP.NET Core
https://learn.microsoft.com/aspnet/core/fundamentals/http-requests
 

HttpClientFactory source code in the 
dotnet/runtime
 GitHub repository
https://github.com/dotnet/runtime/tree/release/7.0/src/libraries/Microsoft.Extensions.Http/
 

Polly (.NET resilience and transient-fault-handling library)
https://thepollyproject.azurewebsites.net/
 
Implement HTTP call retries with exponential backoff 
with IHttpClientFactory and Polly policies 
The recommended approach for retries with exponential backoff is to take advantage of more 
advanced .NET libraries like the open-source 
Polly library

Polly is a .NET library that provides resilience and transient-fault handling capabilities. You can 
implement those capabilities by applying Polly policies such as Retry, Circuit Breaker, Bulkhead 
Isolation, Timeout, and Fallback. Polly targets .NET Framework 4.x and .NET Standard 1.0, 1.1, and 2.0 
(which supports .NET Core and later). 
The following steps show how you can use Http retries with Polly integrated into 
IHttpClientFactory
, which is explained in the previous section. 


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 

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   257   258   259   260   261   262   263   264   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin