Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Container.CreateItemAsync替换为Container.CreateTransactionalBatch_C#_Azure Cosmosdb - Fatal编程技术网

C# 将Container.CreateItemAsync替换为Container.CreateTransactionalBatch

C# 将Container.CreateItemAsync替换为Container.CreateTransactionalBatch,c#,azure-cosmosdb,C#,Azure Cosmosdb,我想使用Container.CreateTransactionlBatch进行批量插入操作。目前,我正在使用container.CreateItemAsync方法执行此操作,这需要更多的时间 是否可以将container.CreateItemAsync替换为container.CreateTransactionalBatch private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, Subscri

我想使用Container.CreateTransactionlBatch进行批量插入操作。目前,我正在使用container.CreateItemAsync方法执行此操作,这需要更多的时间

是否可以将container.CreateItemAsync替换为container.CreateTransactionalBatch

private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
        {
            List<Task> bulkOperations = new List<Task>();
            foreach (var notificationCategory in notificationCategoryTypes)
            {
                var notificationTypes = Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);

                foreach (var notificationType in notificationTypes)
                {
                    foreach (var payerAccountSubscriptions in command.Subscriptions)
                    {
                        if (payerAccountSubscriptions.AccountNumbers?.Any() ?? false)
                        {
                            foreach (var accountNumber in payerAccountSubscriptions.AccountNumbers.Where(a => !string.IsNullOrEmpty(a)))
                            {
                                bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                      payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
                                      command.UserId));
                            }
                        }
                        else
                        {
                            bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
                                command.UserId));

                        }
                    }
                }
            }
            await Task.WhenAll(bulkOperations);
        }

 public async Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
            int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
        {
            var eventType = Utility.GetEventType(notificationType);

            var subscriptionBase = new Subscription
            {
                Id = Guid.NewGuid(),
                IsActive = true,
                Action = subscriptionAction,
                ActionDesc = subscriptionAction.ToString(),
                Version = (int)SubscriptionVersion.V2,
                NotificationType = notificationType,
                NotificationTypeDesc = notificationType.ToString(),
                EventType = eventType,
                EventTypeDesc = eventType.ToString(),
                ColCoId = colCoId,
                PayerNumber = payerNumber,
                AccountNumber = accountNumber,
                CardId = cardId,
                DistributionGroups = new List<string> { userRole.ToString() },
                DistributionUserIds = new List<string> { userId }
            };
            return await CreateItemAsync(subscriptionBase);
        }

public async Task<ItemResponse<T>> CreateItemAsync(T item)
        {
            return await _container.CreateItemAsync<T>(item);
        }
private async Task AddSubscription(启用或禁用subscriptionCommand命令、SubscriptionAction SubscriptionAction、IList notificationCategoryTypes)
{
列表操作=新列表();
foreach(notificationCategoryTypes中的var notificationCategory)
{
var notificationTypes=Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);
foreach(notificationTypes中的var notificationType)
{
foreach(命令中的var payerAccountSubscriptions.Subscriptions)
{
if(payerAccountSubscriptions.AccountNumbers?Any()?false)
{
foreach(payerAccountSubscriptions.AccountNumbers.Where(a=>!string.IsNullOrEmpty(a))中的var accountNumber)
{
bulkOperations.Add(_repository.Create)(subscriptionAction,notificationType,
payerAccountSubscriptions.ColCoId、payerAccountSubscriptions.PayerNumber、accountNumber、command.UserRole、,
command.UserId);
}
}
其他的
{
bulkOperations.Add(_repository.Create)(subscriptionAction,notificationType,
payerAccountSubscriptions.ColCoId,payerAccountSubscriptions.PayerNumber,null,command.UserRole,
command.UserId);
}
}
}
}
等待任务时(批量操作);
}
公共异步任务创建(SubscriptionAction SubscriptionAction,NotificationType NotificationType,
int colCoId,string payerNumber,string accountNumber,UserRole UserRole,string userId,string cardId=null)
{
var eventType=Utility.GetEventType(notificationType);
var subscriptionBase=新订阅
{
Id=Guid.NewGuid(),
IsActive=true,
动作=订阅动作,
ActionDesc=subscriptionAction.ToString(),
版本=(int)SubscriptionVersion.V2,
NotificationType=NotificationType,
NotificationTypeDesc=notificationType.ToString(),
EventType=EventType,
EventTypeDesc=eventType.ToString(),
ColCoId=ColCoId,
PayerNumber=PayerNumber,
AccountNumber=AccountNumber,
卡迪,
DistributionGroups=新列表{userRole.ToString()},
DistributionSerids=新列表{userId}
};
返回等待CreateItemAsync(subscriptionBase);
}
公共异步任务CreateItemAsync(T项)
{
return wait_container.CreateItemAsync(item);
}

有两种不同的东西

  • TransactionalBatch适用于需要将一组操作作为单个工作单元提交的场景,这些操作要么全部成功,要么全部失败。如果其中一个批处理操作失败,则所有批处理操作都将失败。参考:
  • 批量操作,用于吞吐量优化的场景(您希望充分利用所提供的吞吐量,或者尽可能多地使用它来推送中到高容量的数据)。参考:
根据您正在共享的代码,您似乎正在尝试使用批量?如果是这样的话,您需要确保您的
CosmosClient
实例是使用启用批量的
CosmosClientOptions
创建的:

CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connectionString, options);
在您的代码中,您可能还希望从其他方法中删除
wait
,只返回
任务,例如:

public Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
    int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
{
    // ... OTHER CODE
    return CreateItemAsync(subscriptionBase);
}

public Task<ItemResponse<T>> CreateItemAsync(T item)
{
    return _container.CreateItemAsync<T>(item);
}
公共任务创建(SubscriptionAction SubscriptionAction,NotificationType NotificationType, int colCoId,string payerNumber,string accountNumber,UserRole UserRole,string userId,string cardId=null) { //…其他代码 返回CreateItemAsync(subscriptionBase); } 公共任务CreateItemAsync(T项) { 返回_container.CreateItemAsync(item); }
有两种不同的东西

  • TransactionalBatch适用于需要将一组操作作为单个工作单元提交的场景,这些操作要么全部成功,要么全部失败。如果其中一个批处理操作失败,则所有批处理操作都将失败。参考:
  • 批量操作,用于吞吐量优化的场景(您希望充分利用所提供的吞吐量,或者尽可能多地使用它来推送中到高容量的数据)。参考:
根据您正在共享的代码,您似乎正在尝试使用批量?如果是这样的话,您需要确保您的
CosmosClient
实例是使用启用批量的
CosmosClientOptions
创建的:

CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connectionString, options);
在您的代码中,您可能还希望从其他方法中删除
wait
,只返回
任务,例如:

public Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
    int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
{
    // ... OTHER CODE
    return CreateItemAsync(subscriptionBase);
}

public Task<ItemResponse<T>> CreateItemAsync(T item)
{
    return _container.CreateItemAsync<T>(item);
}
公共任务创建(SubscriptionAction SubscriptionAction,NotificationType NotificationType, int colCoId,string payerNumber,string accountNumber,UserRole UserRole,string userId,string cardId=null) { //…其他代码 返回CreateItemAsync(subscriptionBase); } 公共任务CreateItemAsync(T项) { 返回_container.CreateItemAsync(item); }
您是在尝试批量插入还是