4

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.

Teeknow
  • 1,015
  • 2
  • 17
  • 39
  • 2
    See http://razingtheivorytower.blogspot.com/2012/09/optimistic-concurrency-with-azure-tables.html. You can check for `StatusCode = 412`, which is still a magic number, but not matching error strings. – mellamokb Apr 02 '14 at 17:46
  • Ah I missed that. If you want to throw the link in an answer I'll accept it. It's exactly what I want. – Teeknow Apr 02 '14 at 19:11

1 Answers1

2

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--);
            }
        }
    }
gabics
  • 250
  • 1
  • 14