【问题标题】:Error creating Azure Table using C#使用 C# 创建 Azure 表时出错
【发布时间】:2017-09-14 10:31:33
【问题描述】:
按照本文 (https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet) 中的步骤,我创建了一个控制台应用程序,并将以下代码添加到 main 方法中:
string connection = "DefaultEndpointsProtocol=https;AccountName=MyStorageAccountName;AccountKey=MyAccountKey;EndpointSuffix=core.windows.net";
CloudStorageAccount account;
if (!CloudStorageAccount.TryParse(connection, out account))
{
throw new Exception("Unable to parse storage account connection string.");
}
CloudTableClient tableClient = account.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("mytable");
// this is the line at which I get the error:
table.CreateIfNotExists();
但是,执行最后一行时出现以下错误:
无法加载文件或程序集“Microsoft.Data.OData,Version=5.6.2.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)
知道为什么我会收到此错误吗?
注意:我编写了针对相同存储帐户但创建队列的类似代码,并且运行良好。
【问题讨论】:
标签:
c#
azure
azure-table-storage
【解决方案1】:
似乎是时候删除解决方案的包文件夹并清理和重建项目了。还要确保实际安装了 Microsoft.Data.OData nuget 包。:)
【解决方案2】:
知道为什么我会收到此错误吗?
由于您已经安装了 WindowAzure.Storage(8.4.0 版) 包,它的一个名为 Microsoft.Data.OData(5.8.2 版) 的依赖项也将被安装。可能有一些其他包引用了 Microsoft.Data.OData(版本 5.6.2),它会导致您在帖子中提到的异常。
要解决此问题,您可以在配置文件(app.config)中添加 assemblyBinding。它将 Microsoft.Data.OData 的所有依赖项绑定到您安装的程序集。 assemblyBinding 的格式如下。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /></startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.8.3.0" newVersion="5.8.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>