Azure DocumentDB:读取会话不适用于输入会话令牌

时间:2017-11-04 18:17:33

标签: c# azure caching azure-web-sites azure-cosmosdb

所以我抛出了这个断断续续的异常。经过研究,我确保我的DocumentClient对象在整个应用程序生命周期中被缓存和使用,并且我已将所有Nuget包更新到最新版本。在本地调试会话中使用Visual Studio 2015。

我正在研究Document内存缓存机制。在某些时候,我想将一个对象插入缓存并检索生成的Document对象。所以首先我将对象添加到DocumentDB。这是我的工作:

await docClient.CreateDocumentAsync(uriCollection, docInit).ConfigureAwait(false);
docCacheEntry = await DirectQueryForDocument(docClient, uriCollection, strDocId).ConfigureAwait(false);

原因是我希望初始化Document属性(例如ETag)。

但是(有时)当我打电话给DirectQueryForDocument()时,它会抛出这个:

DocDBTrace Error: 0 : DocumentClientException with status code: NotFound, message: The read session is not available for the input session token.
ActivityId: 458a5727-98a5-4fef-999a-1fddb68e5cdc, 
ResponseTime: 2017-11-04T17:31:23.7938911Z <truncated>
ResponseTime: 2017-11-04T17:31:23.7938911Z <truncated>
ResponseTime: 2017-11-04T17:31:23.7938911Z <truncated>
ResponseTime: 2017-11-04T17:31:23.8138911Z <truncated>
, Microsoft.Azure.Documents.Common/1.17.91.3, and response headers: {
"Transfer-Encoding": "chunked",
"x-ms-substatus": "1002",
"x-ms-activity-id": "458a5727-98a5-4fef-999a-1fddb68e5cdc",
"Strict-Transport-Security": "max-age=31536000",
"x-ms-gatewayversion": "version=1.17.91.3",
"Date": "Sat, 04 Nov 2017 17:31:23 GMT",
"Server": "Microsoft-HTTPAPI/2.0",
}
DocDBTrace Warning: 0 : Read session not available. Retry using write endpoint.
DocDBTrace Error: 0 : DocumentClientException with status code: NotFound, message: The read session is not available for the input session token.
ActivityId: 9133c4cb-e1eb-4f21-86d3-3301c1e194f8, 
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
, Microsoft.Azure.Documents.Common/1.17.91.3, and response headers: {
"Transfer-Encoding": "chunked",
"x-ms-substatus": "1002",
"x-ms-activity-id": "9133c4cb-e1eb-4f21-86d3-3301c1e194f8",
"Strict-Transport-Security": "max-age=31536000",
"x-ms-gatewayversion": "version=1.17.91.3",
"Date": "Sat, 04 Nov 2017 17:31:23 GMT",
"Server": "Microsoft-HTTPAPI/2.0",
}
DocDBTrace Warning: 0 : Clear the the token for named base request dbs/lps/colls/cache-tables
Exception thrown: 'Microsoft.Azure.Documents.DocumentClientException' in mscorlib.dll
Additional information: The read session is not available for the input session token.
ActivityId: 9133c4cb-e1eb-4f21-86d3-3301c1e194f8, 
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
ResponseTime: 2017-11-04T17:31:24.3539231Z <truncated>
, Microsoft.Azure.Documents.Common/1.17.91.3

以下是DirectQueryForDocument()(抛出异常的地方)的来源

private static async Task<Document> DirectQueryForDocument(DocumentClient docClient, Uri uriCollection, string strId)
{
    var query = docClient.CreateDocumentQuery(uriCollection).Where((x) => x.Id == strId).AsDocumentQuery();
    if (query.HasMoreResults)
    {
        // exception is thrown on next line
        var res = await query.ExecuteNextAsync().ConfigureAwait(false);
        if (res.Count == 1)
        {
            return res.Single();
        }
    }
    return null;
}

显然,我可以捕获异常,只是不将它添加到我的内存缓存中,然后在请求文档时再次尝试下线,但这会破坏缓存的全部目的。

我错过了什么?

ETA: 我刚刚意识到我不需要查询以获取数据库中的Document,我可以这样做以节省大量时间:

var resDoc = await docClient.CreateDocumentAsync(uriCollection, docInit).ConfigureAwait(false);
docCacheEntry = resDoc.Resource;

但是,我仍然想知道为什么会发生这种情况!

0 个答案:

没有答案