This topic describes how to configure a retry mechanism in Alibaba Cloud SDK V2.0 for .NET.
In Alibaba Cloud SDK V2.0, the API request processing logic includes the built-in retry logic for network exceptions. When a network exception occurs, the system automatically retries to reinitiate the request to ensure service stability and reliability. If an error is returned due to a business logic error, such as a parameter error or unavailable resources, the system performs no retries. In this case, you must adjust the request based on the corresponding error information instead of performing retries.
Configuration methods
The retry mechanism that you configure by using RuntimeOptions takes precedence over the default retry mechanism.
Use the default retry mechanism. By default, no retries are performed when exceptions occur. If you enable a retry mechanism and you do not specify the maximum number of retries, up to three retries are performed by default.
Use RuntimeOptions.
using AlibabaCloud.TeaUtil.Models; using Tea; namespace demo { public class Sample { static void Main(string[] args) { var ecsConfig = new AlibabaCloud.OpenApiClient.Models.Config { // Obtain the AccessKey ID from an environment variable. AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Obtain the AccessKey secret from an environment variable. AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), Endpoint = "ecs.cn-beijing.aliyuncs.com" }; var escClient = new AlibabaCloud.SDK.Ecs20140526.Client(ecsConfig); var describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest(); // Use RuntimeOptions to configure a retry mechanism. The setting takes effect only on the requests that use the RuntimeOptions object. var runtimeOptions = new RuntimeOptions(); // Enable the automatic retry mechanism. runtimeOptions.Autoretry = true; // Specify the maximum number of automatic retries. runtimeOptions.MaxAttempts = 3; var response = escClient.DescribeRegionsWithOptions(describeInstancesRequest, runtimeOptions); Console.WriteLine(response.Body.ToMap()); } } }