升级版 SDK 将 Request 中的业务参数和这种运行时参数分开设置避免用户混淆,所以超时的设置升级版 SDK 的超时机制为 RuntimeOption -> Client 设置 -> 默认,优先级依次降低;默认连接超时为5秒,读超时为10秒:
public static void Main(string[] args)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 您的AccessKey ID
AccessKeyId = accessKeyId,
// 您的AccessKey Secret
AccessKeySecret = accessKeySecret,
};
// Client 代理参数设置
// 连接超时默认 5 * 1000 毫秒
config.ConnectTimeout = connectionTimeoutMillis;
// 读超时默认 10 * 1000 毫秒
config.ReadTimeout = readTimeoutMillis;
config.Endpoint = "ecs-cn-hangzhou.aliyuncs.com";
AlibabaCloud.SDK.Ecs20140526.Client client = new AlibabaCloud.SDK.Ecs20140526.Client(config);
// 运行时参数设置,仅对使用了该运行时参数实例的请求有效
RuntimeOptions runtimeOptions = new RuntimeOptions();
runtimeOptions.ConnectTimeout = connectionTimeoutMillis;
runtimeOptions.ReadTimeout = readTimeoutMillis;
DescribeInstancesRequest request = new DescribeInstancesRequest();
// 通过 SDK Core 执行实际的 Http 请求将返回的结果序列化为 SDK 的 Response 结构
var response = client.DescribeInstancesWithOptions(request, runtimeOptions);
System.Console.WriteLine(response.Body.TotalCount);
}