尝试更新集合的IndexingPolicy。使用CreateDocumentCollectionIsNotExistsAsync时,集合定义工作正常,但在尝试使用ReplaceDocumentCollectionAsync更新现有集合的定义时,将抛出DocumentClientException。使用.NET库1.19.1。例外细节是:
DocDBTrace Error: 0 : DocumentClientException with status code NotFound, message: The value '' specified for query '$resolveFor' is invalid., inner exception: null, and response headers: null
DocDBTrace Error: 0 : Operation will NOT be retried. Current attempt 0, Exception: Microsoft.Azure.Documents.NotFoundException: The value '' specified for query '$resolveFor' is invalid., documentdb-dotnet-sdk/1.19.1 Host/64-bit MicrosoftWindowsNT/6.2.9200.0
at Microsoft.Azure.Documents.DocumentServiceRequest..ctor(OperationType operationType, ResourceType resourceType, String path, Stream body, AuthorizationTokenType authorizationTokenType, NameValueCollection headers)
at Microsoft.Azure.Documents.DocumentServiceRequest.Create(OperationType operationType, String relativePath, Resource resource, ResourceType resourceType, AuthorizationTokenType authorizationTokenType, NameValueCollection headers, SerializationFormattingPolicy formattingPolicy, JsonSerializerSettings settings)
at Microsoft.Azure.Documents.Client.DocumentClient.<ReplaceDocumentCollectionPrivateAsync>d__123.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.BackoffRetryUtility`1.<>c__DisplayClass2.<<ExecuteAsync>b__0>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Documents.BackoffRetryUtility`1.<ExecuteRetry>d__1b.MoveNext()
更新了代码。我正在尝试共享创建/修改索引代码,所以我先调用CreateDocumentCollectionIfNotExistAsync,如果集合存在,那么我修改索引策略以匹配最新的。
DocumentCollection appCollection = new DocumentCollection();
appCollection.Id = CosmosDbCollectionName;
// Set the index policy
var rrdc = await cdbClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(CosmosDbDatabaseName), appCollection);
if (true == rrdc.StatusCode.IsSuccessCode())
{
// If OK was returned, the collection already existed.
if (HttpStatusCode.OK == rrdc.StatusCode)
{
var rr = await _cdbClient.ReplaceDocumentCollectionAsync(appCollection).ConfigureAwait(false);
if (false == rr.StatusCode.IsSuccessCode())
return false;
}
}
您需要将从CreateDocumentCollectionIfNotExistsAsync
返回的DocumentCollection资源实例传递给ReplaceDocumentCollectionAsync
。
例如:
DocumentCollection appCollectionSpec = new DocumentCollection();
appCollection.Id = CosmosDbCollectionName;
// Set the index policy
ResourceResponse<DocumentCollection> rrdc = await cdbClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(CosmosDbDatabaseName), appCollectionSpec);
if (true == rrdc.StatusCode.IsSuccessCode())
{
// If OK was returned, the collection already existed.
if (HttpStatusCode.OK == rrdc.StatusCode)
{
var rr = await _cdbClient.ReplaceDocumentCollectionAsync(rrdc.Resource).ConfigureAwait(false);
if (false == rr.StatusCode.IsSuccessCode())
return false;
}
}
我不相信传递给CreateDocumentCollectionIfNotExists
的DocumentCollection实例被调用修改,因为它被视为'spec'对象。替换操作需要仅由服务器返回的资源填充的内部元数据。