如何为ReadDocumentAsync设置超时(Azure Cosmos Db)

时间:2018-05-15 18:56:42

标签: azure-cosmosdb

我使用以下内容将超时设置为1秒:

DocumentClient documentClient = new DocumentClient(new Uri(DocumentDbEndpointUrl),
            DocumentDbKey,
            new ConnectionPolicy { 
ConnectionMode = ConnectionMode.Gateway, 
ConnectionProtocol = Protocol.Tcp,
RequestTimeout = new TimeSpan(0, 0, 0, 1) });

然后阅读:

return await documentClient.ReadDocumentAsync<Response>(docUri, 
new RequestOptions { PartitionKey = new PartitionKey(key)});

但它并不尊重我设置的超时值,有时超时&gt; 5秒。

1 个答案:

答案 0 :(得分:1)

尝试使用类似的东西,

        var connectionPolicy = new ConnectionPolicy
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp,
            RequestTimeout = new TimeSpan(1, 0, 0),
            MaxConnectionLimit = 1000,
            RetryOptions = new RetryOptions
            {
                MaxRetryAttemptsOnThrottledRequests = 10,
                MaxRetryWaitTimeInSeconds = 60
            }
        };

        _client = new DocumentClient(new Uri(_uri), _key, connectionPolicy);