예제 #1
0
        public async Task SetAsync(IEnumerable <T> instances)
        {
            ThrowWhenCloudTableDoesNotExist();
            var batches         = new List <TableBatchOperation>();
            var partitionGroups = instances.GroupBy(arg => arg.PartitionKey).ToArray();

            foreach (var group in partitionGroups)
            {
                var groupList = group.ToArray();
                var offSet    = 100;
                var entities  = groupList.Take(offSet).ToArray();
                while (entities.Any())
                {
                    var tableBatchOperation = new TableBatchOperation();
                    foreach (var entity in entities)
                    {
                        var tableEntity = new TableStorageEntityAdapter <T>(entity)
                        {
                            ETag = "*"
                        };
                        tableBatchOperation.Add(TableOperation.InsertOrReplace(tableEntity));
                    }
                    batches.Add(tableBatchOperation);
                    entities = groupList.Skip(offSet).Take(100).ToArray();
                    offSet  += 100;
                }
            }
            await Task.WhenAll(batches.Select(CloudTable.ExecuteBatchAsync));
        }
예제 #2
0
        public async Task <bool> SetAsync(T instance)
        {
            ThrowWhenCloudTableDoesNotExist();
            bool result;

            try
            {
                var tableEntity = new TableStorageEntityAdapter <T>(instance)
                {
                    ETag = "*"
                };
                var tableResult = await CloudTable.ExecuteAsync(TableOperation.InsertOrReplace(tableEntity));

                result = tableResult.HttpStatusCode == (int)HttpStatusCode.NoContent;
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound)
                {
                    result = false;
                }
                else
                {
                    throw;
                }
            }
            return(result);
        }