All Products
Search
Document Center

Alibaba Cloud SDK:Configure a retry mechanism

Last Updated:Jun 19, 2023

In Alibaba Cloud Darabonba SDK for .NET, you can configure a retry mechanism for network exceptions. You cannot configure a retry mechanism for business exceptions. You can configure a retry mechanism by using RuntimeOptions. The retry mechanism that you configure by using RuntimeOptions takes precedence over the default retry mechanism. If the default retry mechanism is used, no retries are performed when exceptions occur. If you enable a retry mechanism and you do not specify a retry threshold, a maximum of three retries are performed by default.

public static void Main(string[] args)
{
    AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
    {
        // Your AccessKey ID.
        AccessKeyId = accessKeyId,
        // Your AccessKey secret.
        AccessKeySecret = accessKeySecret,
    };
    config.Endpoint = "ecs-cn-hangzhou.aliyuncs.com";
    AlibabaCloud.SDK.Ecs20140526.Client client = new AlibabaCloud.SDK.Ecs20140526.Client(config);

    // Configure a retry mechanism by using runtime parameters. The setting takes effect only on the requests that use the RuntimeOptions instance.
    RuntimeOptions runtimeOptions = new RuntimeOptions();
    // Enable automatic retries.
    runtimeOptions.Autoretry = true;
    // Specify a threshold value for the number of automatic retry attempts.
    runtimeOptions.MaxAttempts = 3;

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    // Process HTTP requests to serialize the return result into a response object in the SDK based on the core library.
    var response = client.DescribeInstancesWithOptions(request, runtimeOptions);
    System.Console.WriteLine(response.Body.TotalCount);
}