我正在使用以下代码从DocumentDB中获取记录。
处理程序:
public async Task HandleAsync(EnableOrDisableSubscriptionCommand command, ILogger log)
{
try
{
//delete all existing subscriptions for this user
await DeleteAllExistingSubscriptions(command.UserId);
//enable new subscriptions
if (command.UiNotifications.Any())
await AddSubscription(command, SubscriptionAction.UiNotification, command.UiNotifications);
}
catch (Exception ex)
{
}
}
/// <summary>
/// Delete all existing subscriptions
/// </summary>
/// <param name="userUUId"></param>
/// <returns></returns>
private async Task DeleteAllExistingSubscriptions(string userUUId)
{
var userSubscriptions = await _subscriptionBaseRepository
.GetItemsAsync(x => x.DistributionUserIds.Contains(userUUId), o => o.PayerNumber);
if (userSubscriptions.Any())
{
var deleteTask = userSubscriptions
.Select(userSubscription => _subscriptionBaseRepository.DeleteItemAsync(userSubscription.Id.ToString(), userSubscription.PayerNumber)).ToArray();
Task.WaitAll(deleteTask);
}
}
Cosmos DB存储库方法GetItemAsync:
public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> orderByDesc, int takeCount = -1)
{
var criteria = _client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), new FeedOptions { EnableCrossPartitionQuery = true })
.Where(predicate)
.OrderByDescending(orderByDesc)
.AsDocumentQuery();
IDocumentQuery<T> query = criteria;
List<T> results = new List<T>();
while (query.HasMoreResults)
{
if (takeCount > -1 && results.Count >= takeCount)
{
break;
}
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
Cosmos DB存储库方法:DeleteItemAsync:
public async Task DeleteItemAsync(string id, string partitionKey)
{
await _client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
}
我遇到以下错误:
{“跨分区查询是必需的,但已禁用。请设置 x-ms-document-db-query-enablecrosspartition为true,请指定 x-ms-documentdb-partitionkey,或修改查询以避免这种情况 \ r \ nActivityId:6bd7dd7a-a29c-40c1-a0d2-4599b2d137e8, Microsoft.Azure.Documents.Common / 2.2.0.0,Windows / 10.0.17763 documentdb-netcore-sdk / 2.10.3“}
堆栈跟踪:
at Microsoft.Azure.Documents.GatewayStoreClient.<ParseResponseAsync>d__8.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.GatewayStoreClient.<InvokeAsync>d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.GatewayStoreModel.<ProcessMessageAsync>d__8.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Client.DocumentClient.<ProcessRequestAsync>d__159.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Client.DocumentClient.<ProcessRequestAsync>d__158.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Query.QueryPlanRetriever.<GetQueryPlanThroughGatewayAsync>d__5.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Query.DocumentQueryExecutionContextFactory.<CreateDocumentQueryExecutionContextAsync>d__3.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Azure.Documents.Linq.DocumentQuery`1.<ExecuteNextPrivateAsync>d__36`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at FleetHub.Notifications.Domain.Repositories.CosmosDBRepository`1.<GetItemsAsync>d__9.MoveNext() in C:\notifications_v2\FleetHub.Notifications\FleetHub.Notifications.Domain\Repositories\CosmosDBRepository.cs:line 106
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at FleetHub.Notifications.Commands.Handlers.Subscribers.EnableOrDisableSubscriptionCommandHandler.<DeleteAllExistingSubscriptions>d__3.MoveNext() in C:\notifications_v2\FleetHub.Notifications\FleetHub.Notifications.Commands\Handlers\Subscriptions\EnableOrDisableSubscriptionCommandHandler.cs:line 45
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at FleetHub.Notifications.Commands.Handlers.Subscribers.EnableOrDisableSubscriptionCommandHandler.<HandleAsync>d__2.MoveNext() in C:\notifications_v2\FleetHub.Notifications\FleetHub.Notifications.Commands\Handlers\Subscriptions\EnableOrDisableSubscriptionCommandHandler.cs:line 26