|
|
Posted on 5/28/2019 11:54:53 PM
|
|
|
|

This post was last edited by Kongcida on 2019-5-29 11:16
preface In a project, there will be problems with project operation due to uncontrollable reasons. For example, a server failure, a service crash, network outage, etc. Encountering some retryable errors, we can use Polly —— . .NET Elastic and Transient Fault Handling Libraries. Project official website address (The hyperlink login is visible.)。 Today this article will document Polly's bad retry strategy.
Environment & Tools
>VS 2017
>Windows 10 system
Text
1: Create a new console application.
2: Quote Polly. Available for download in the NuGet package
3: Paste the code
4: F6 generates. Then find the generated .exe file under the debug folder – > double-click to run.
You can see the output message: Retried three times.
When F5 is debugging, you can also see that this method has been done three times.
Polly's retry strategy is explained in detail
1: Exception type
In this code, the :handle contains the exception type to be retried. That is, when the executed code produces a specific exception, the retry function is executed. Otherwise, no retry will be performed. Because some exceptions do not require a retry.
Of course, we can fill in a number of specific exception types here to retry.
The DivideByZeroException here represents an exception that attempts to divide by zero. SqlException indicates an exception that is raised when Sql Server returns an error or warning. For example, database connection failure, SQL execution error, etc. SmtpException indicates an exception raised by the mail service. For example, the sending failed, the network connection failed, and the email account information was abnormal.
For specific exception types, please refer to Microsoft's official documentation:
2: Retry type
1) The first type of retry is a direct given number of retries. The following code is only retried 3 times.
We can call the relevant actions performed on each retry when retrying. The following code is another commission later. When performing a retry, the number of retries and the reason for the exception are printed.
We can also keep the code retrying until it succeeds.
2) The second type of retry is to retry at intervals. The following code means to retry every 2 seconds, 7 seconds, or 10 seconds. Retry to 10 seconds. If the execution is not successful, end the retry.
We can also try again at intervals until the retry is successful. This requires the calculation of a function.
The above code refers to the power retry of indirect 2. For example, if the execution fails, the first time will be retried at intervals of 2 seconds, the second time will be retried at intervals of 4 seconds, and the third time will be retried at intervals of 8 seconds......... And so on. Until execution is successful.
Epilogue
Note: The exception types in the above code Handle should be filled in according to the actual situation in your actual project. Not all exceptions require a retry.
The exception type is the base class for all exceptions. It is best not to fill this in the handle, otherwise, it will have very serious consequences.
|
Previous:. Net integration PayPal IPN return value has always been INVALIDNext:C# Usage of yield keywords
|