Jas*_*des 2 azure azure-storage azure-table-storage
我正在使用WindowsAzure.Storage nuget软件包版本9.0.0。
Install-Package WindowsAzure.Storage -Version 9.0.0
Run Code Online (Sandbox Code Playgroud)
以下代码(table.CreateIfNotExistsAsync()
)引发错误:远程服务器返回错误:(409)冲突。
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;
Run Code Online (Sandbox Code Playgroud)
如果检查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
Run Code Online (Sandbox Code Playgroud)
这段代码可以正常工作:
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;
Run Code Online (Sandbox Code Playgroud)
我知道我有已经存在的表,并且当前未删除它们。为什么会出现此错误?由于该表确实存在,因此我希望它能够执行和存在性检查,并且只返回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;
}
Run Code Online (Sandbox Code Playgroud)
存储连接字符串如下所示:
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=something;AccountKey=somekeygoeshere==" />
Run Code Online (Sandbox Code Playgroud)
小智 7
CreateIfNotExists
引发异常。
方法CloudBlobContainer.CreateIfNotExists
或的实现发生了变化CloudTable.CreateIfNotExists
。
7.1.2存储客户端库中的实现如下:
进行调用以查看存储容器或表是否存在(HTTP HEAD请求)。
如果存在,则不执行任何操作。
如果不存在,则调用创建容器或表。
在存储客户端库8.1.1中,实现如下:
调用以创建容器或表(HTTP PUT请求)。
如果由于容器或表已存在而返回错误(HTTP 409),则不执行任何操作。错误已得到处理。
如果容器或表不存在,则创建将成功。
我检查了9.0,但该行为仍然存在
一个不错的解决方法可能是:
if (!container.Exists())
{
container.CreateIfNotExists();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3404 次 |
最近记录: |