Search code examples
azureconcurrencyazure-table-storageoptimistic-concurrency

Azure Table Storage Concurrency Issue


It seems like concurrency conflicts with Azure throw an exception with a message containing the error code 412. Is there a good way to tell that an exception is thrown due to a concurrency problem other than checking if the error message of a StorageException contains 412 in the message? This seems like a really stringly typed approach.


Solution

  • Using "RequestInformation.HttpStatusCode seems to work for me

        try
        {
            TableOperation operation = TableOperation.Merge(ent);
            retval = await table.ExecuteAsync(operation);
        }
        catch (StorageException sex)
        {
            if (sex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
            {
                TableOperation retrieveOperation = TableOperation.Retrieve<BotSetting>(ent.PartitionKey, ent.RowKey);
                TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
                if (retrievedResult.Result != null)
                {
                    BotSetting bs = retrievedResult.Result as BotSetting;
                    retval = await TryMerge(table, bs, tries--);
                }
            }
        }