【发布时间】:2018-07-31 07:15:55
【问题描述】:
我使用的是 WindowsAzure.Storage nuget 包版本 9.0.0。
Install-Package WindowsAzure.Storage -Version 9.0.0
以下代码 (table.CreateIfNotExistsAsync()) 抛出错误:远程服务器返回错误:(409) Conflict。
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(tableName);
try
{
if (await table.CreateIfNotExistsAsync())
{
log.Info(string.Format("Created Table named: {0}", tableName));
}
}
catch (StorageException)
{
log.Error("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
throw;
}
return table;
如果我检查 StorageException 详细信息,我会发现以下消息:指定的表已经存在。
堆栈跟踪:
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 57
这段代码运行良好:
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(tableName);
try
{
//if (await table.CreateIfNotExistsAsync())
//{
// log.Info(string.Format("Created Table named: {0}", tableName));
//}
if (!table.Exists())
{
await table.CreateAsync();
log.Info(string.Format("Created Table named: {0}", tableName));
}
}
catch (StorageException)
{
log.Error("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
throw;
}
return table;
我知道我有一些表已经存在并且它们当前没有被删除。为什么我会收到此错误?由于该表确实存在,我希望它能够执行存在性检查并且只返回 true,而不是引发存储异常。
编辑:这就是我创建 CloudStorageAccount 的方式
public static CloudStorageAccount CreateStorageAccountFromConnectionString(string storageConnectionString)
{
CloudStorageAccount storageAccount;
try
{
storageAccount = CloudStorageAccount.Parse(storageConnectionString);
}
catch (FormatException fe)
{
log.Error("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the application.", fe);
throw;
}
catch (ArgumentException ae)
{
log.Error("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the sample.", ae);
throw;
}
return storageAccount;
}
存储连接字符串如下所示:
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=something;AccountKey=somekeygoeshere==" />
【问题讨论】:
标签: azure azure-storage azure-table-storage