升级版 SDK 对 OpenAPI 请求中遇到网络问题的情况会进行重试,对于业务报错则不会重试。在请求中通过运行时参数配置重试,优先级为 RuntimeOption -> 默认;默认不会进行重试,如果打开重试配置未配置重试次数,则默认最多重试3次:
public static void Main(string[] args)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 您的AccessKey ID
AccessKeyId = accessKeyId,
// 您的AccessKey Secret
AccessKeySecret = accessKeySecret,
};
config.Endpoint = "ecs-cn-hangzhou.aliyuncs.com";
AlibabaCloud.SDK.Ecs20140526.Client client = new AlibabaCloud.SDK.Ecs20140526.Client(config);
// 运行时参数设置,仅对使用了该运行时参数实例的请求有效
RuntimeOptions runtimeOptions = new RuntimeOptions();
// 开启自动重试机制
runtimeOptions.Autoretry = true;
// 设置自动重试次数
runtimeOptions.MaxAttempts = 3;
DescribeInstancesRequest request = new DescribeInstancesRequest();
// 通过 SDK Core 执行实际的 Http 请求将返回的结果序列化为 SDK 的 Response 结构
var response = client.DescribeInstancesWithOptions(request, runtimeOptions);
System.Console.WriteLine(response.Body.TotalCount);
}