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;
                }
            }
        }