Ejemplo n.º 1
0
        public async Task UpsertEntity <T>(string tableName, T data) where T : class, ITableItem
        {
            try
            {
                // Convert POCO to ITableEntity object using TableEntityConvert.ToTableEntity()
                var tableEntity = TableEntityConvert.ToTableEntity(data);

                // Save the new or updated entity in the Storage
                var operation = TableOperation.InsertOrReplace(tableEntity);

                var table  = CloudTableClient.GetTableReference(tableName);
                var result = await table.ExecuteAsync(operation);

                // Getting the HTTP result to detect if a conflict has occurred when trying to upsert the file to allow the client to specifically deal with this.
                if (result.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
                {
                    Logger?.LogWarning("Warning: Conflict error while upserting entity.");
                    throw new ConflictException("conflict while trying to upsert entity");
                }
            }
            catch (StorageException st)
                when(st.RequestInformation.HttpStatusCode == 400)
                {
                    Logger?.LogError($"Bad Request to table storage ({tableName}) - check modal properties are all set: {JsonConvert.SerializeObject(data)}");
                    throw new InvalidOperationException("Upsert data is badly formed and resulted in a 400 Bad Request response from TableStorage");
                }
            catch (Exception e)
            {
                Logger?.LogError(e, $"Error: {e.Message} occurred, table name: {tableName} with generic data");
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task UpsertEntities <T>(string tableName, List <T> data, int batchSize = 100) where T : class, ITableItem
        {
            try
            {
                var table          = CloudTableClient.GetTableReference(tableName);
                var batchOperation = new TableBatchOperation();
                batchSize = batchSize > 100 ? 100 : batchSize;

                var lastUsedPartitionKey = string.Empty;

                for (int i = 0; i < data.Count; i++)
                {
                    var entry    = TableEntityConvert.ToTableEntity(data[i]);
                    var newBatch = (lastUsedPartitionKey != entry.PartitionKey && lastUsedPartitionKey.Length > 0);

                    if (newBatch)
                    {
                        await table.ExecuteBatchAsync(batchOperation);

                        batchOperation.Clear();
                        batchOperation.InsertOrReplace(entry);
                    }
                    else
                    {
                        batchOperation.InsertOrReplace(entry);

                        // Execute the batch operation if we reach our batch limit or its the end of the list.
                        if (batchOperation.Count >= batchSize || i == data.Count - 1)
                        {
                            await table.ExecuteBatchAsync(batchOperation);

                            batchOperation.Clear();
                        }
                    }

                    lastUsedPartitionKey = entry.PartitionKey;
                }

                if (batchOperation.Count > 0)
                {
                    await table.ExecuteBatchAsync(batchOperation);

                    batchOperation.Clear();
                }
            }
            catch (StorageException st)
                when(st.RequestInformation.HttpStatusCode == 400)
                {
                    Logger?.LogError($"Bad Request to table storage ({tableName}) - check modal properties are all set: {JsonConvert.SerializeObject(data)}");
                    throw new InvalidOperationException("Upsert data is badly formed and resulted in a 400 Bad Request response from TableStorage");
                }
            catch (Exception e)
            {
                Logger?.LogError(e, $"Error {e.Message} occurred upserting multiple, table name: {tableName} with generic data");
                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task AddOrUpdateCustomer(Customer customer)
        {
            // Convert POCO to ITableEntity object using TableEntityConvert.ToTableEntity()
            var tableEntity = TableEntityConvert.ToTableEntity(customer);

            // Save the new or updated entity in the Storage
            var operation = TableOperation.InsertOrReplace(tableEntity);
            await _cloudTable.ExecuteAsync(operation);
        }