From 1c260c4e4d42fe247e74ee1cc32274205492617c Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Wed, 4 May 2022 17:34:32 -0300 Subject: [PATCH] Retry PutAsync if table not found --- src/TableStorage/TableRepository`1.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/TableStorage/TableRepository`1.cs b/src/TableStorage/TableRepository`1.cs index 41bca03..7f5b9cd 100644 --- a/src/TableStorage/TableRepository`1.cs +++ b/src/TableStorage/TableRepository`1.cs @@ -165,9 +165,19 @@ pair.Value is DateOnly date ? values[nameof(ITableEntity.PartitionKey)] = partitionKey; values[nameof(ITableEntity.RowKey)] = rowKey; - var result = await table.UpsertEntityAsync(new TableEntity(values), UpdateMode, cancellation) - .ConfigureAwait(false); - + try + { + await table.UpsertEntityAsync(new TableEntity(values), UpdateMode, cancellation) + .ConfigureAwait(false); + } + catch (RequestFailedException e) when (e.ErrorCode == "TableNotFound") + { + // Retry after ensuring table exists. + await table.CreateIfNotExistsAsync(); + await table.UpsertEntityAsync(new TableEntity(values), UpdateMode, cancellation) + .ConfigureAwait(false); + } + return (await GetAsync(partitionKey, rowKey, cancellation).ConfigureAwait(false))!; }