如何更改 Azure 队列存储 QueueClient.CreateIfNotExists() 的默认超时?

How to change the default timeout for an Azure Queue storage QueueClient.CreateIfNotExists()?

我正在尝试使用 Azure 队列的 v12 SDK。

当我创建队列实例时,我在应用程序启动时做的第一件事是查看队列是否存在。 Based on the typical documentation examples:

// Get the connection string from app settings
string connectionString = ConfigurationManager.AppSettings["storageConnectionString"];

// Instantiate a QueueClient which will be used to create and manipulate the queue
QueueClient queueClient = new QueueClient(connectionString, "myqueue");

// Create the queue
queueClient.CreateIfNotExists();

这很好..但是..如果代码无法访问队列存储(例如错误的连接字符串/本地主机存储模拟器尚未 100% 启动等)..然后它会挂起很长时间.. 在我的 Polly 代码因为它的“重试策略”而启动之前。

问题:

Is there a way to let the client fail/quit after 5 seconds instead of me having to wait 30 or 60 seconds (like that's some default setting, deep down).

请尝试下面的代码。在新SDK中设置请求超时的方法有点复杂。在代码中,我强制请求在 10 毫秒后超时,并指示 SDK 不要重试请求 (options.Retry.MaxRetries = 0;)

    static void Main(string[] args)
    {
        HttpClient httpClient = new HttpClient()
        {
            Timeout = TimeSpan.FromMilliseconds(10)
        };
        var transport = new HttpClientTransport(httpClient);
        QueueClientOptions options = new QueueClientOptions()
        {
            Transport = transport,
        };
        options.Retry.MaxRetries = 0;
        var queueClient = new QueueClient(connectionString, "test", options);
        queueClient.CreateIfNotExists();
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
    }

Does the client auto-retry? If yes, this means I don't need my polly code...

是的。这是默认的重试策略: