This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3195|Reply: 0

[Source] .NET/C# Simple Use Polly tutorial

[Copy link]
Posted on 7/26/2023 8:00:59 PM | | | |
Requirements: The previous project has been using Polly for the HttpClient retry mechanism, due to network fluctuations or the project may not be able to provide services normally during the release period, using Polly under the microservice architecture improves the availability of the service, and will not cause the entire transaction to fail due to a short exception, but requires interface idempotential.

Polly is a .NET resilient and transient fault handling library that allows developers to express policies such as retries, circuit breakers, timeouts, bulkhead isolation, rate limiting, and fallbacks in a smooth and thread-safe manner.

GitHub address:The hyperlink login is visible.

First, create a new website interface, and the normal status code will be returned only after the third request, the code is as follows:


.NET console app

Create a new console application and use the HttpClient request interface to attempt to request an error as follows:



Introducing Polly, adding a retry mechanism, the nuget command is as follows:

The code is as follows:

Retried 3 times, 4th request succeeded, successfully obtains the returned data, as shown in the figure below:



ASP.NET Core apps

For ASP.NET Core projects, you can directly reference Microsoft.Extensions.Http.Polly, as follows:

The code is as follows:

Reference:

The hyperlink login is visible.
The hyperlink login is visible.
(End)





Previous:.NET/C# uses Refit to request an enum parameter enum type error
Next:PowerShell compresses folders based on Compress-Archive
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com