Ejemplo n.º 1
0
        public static async T.Task <int> GetNextId(this CloudUtilities u, string category, string usage, CancellationToken token)
        {
            var idsTable = u.GetIdsTable();

            while (true)
            {
                try
                {
                    int currentId = 1;
                    var entity    = await idsTable.RetrieveJsonTableEntityAsync(category, usage, token);

                    TableResult result;
                    if (entity == null)
                    {
                        entity = new JsonTableEntity()
                        {
                            PartitionKey = category, RowKey = usage,
                        };
                        entity.PutObject(currentId);
                        result = await idsTable.InsertAsync(entity, token);
                    }
                    else
                    {
                        currentId = entity.GetObject <int>() + 1;
                        entity.PutObject(currentId);
                        result = await idsTable.ReplaceAsync(entity, token);
                    }

                    // concurrency failure or conflict
                    if (result.IsConflict())
                    {
                        continue;
                    }

                    return(currentId);
                }
                catch (StorageException ex)
                {
                    // concurrency failure or conflict
                    if (ex.IsConflict())
                    {
                        continue;
                    }
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task <int> GetNextId(this CloudUtilities u, string category, string usage, CancellationToken token)
        {
            var idsTable = u.GetIdsTable();

            while (true)
            {
                try
                {
                    var result = await idsTable.ExecuteAsync(TableOperation.Retrieve <JsonTableEntity>(category, usage), null, null, token);

                    int currentId = 1;
                    if (result.Result is JsonTableEntity id)
                    {
                        currentId = id.GetObject <int>() + 1;
                    }
                    else
                    {
                        id = new JsonTableEntity()
                        {
                            PartitionKey = category, RowKey = usage,
                        };
                    }

                    id.JsonContent = JsonConvert.SerializeObject(currentId);

                    var updateResult = await idsTable.ExecuteAsync(TableOperation.InsertOrReplace(id), null, null, token);

                    // concurrency failure or conflict
                    if (updateResult.HttpStatusCode == 412 || updateResult.HttpStatusCode == 409)
                    {
                        continue;
                    }

                    return(currentId);
                }
                catch (StorageException ex)
                {
                    // concurrency failure or conflict
                    if (ex.RequestInformation.HttpStatusCode != 412 && ex.RequestInformation.HttpStatusCode != 409)
                    {
                        throw;
                    }
                }
            }
        }