c# - 无法加载文件或程序集 'Microsoft.Azure.Documents.Client - Azure-Table-Api

标签 c# asp.net-core azure-table-storage

我正在尝试将 Azure Table Api 与 dotnet 核心一起使用,但我一直收到此异常:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Documents.Client, Version=1.20.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

   at Microsoft.Azure.CosmosDB.Table.CloudTableClient..ctor(StorageUri storageUri, StorageCredentials credentials, TableConnectionPolicy connectionPolicy, Nullable`1 desiredConsistencyLevel)
   at Microsoft.Azure.CosmosDB.Table.CloudStorageAccountExtensions.CreateCloudTableClient(CloudStorageAccount account, TableConnectionPolicy connectionPolicy, Nullable`1 consistencyLevel)
   at Testing.Program.Main(String[] args) in /Desktop/Repos/TestingWow/Testing/Program.cs:line 22

显然这是一个 common exception message .我把我的简单代码放在 GitHub 上万一。我想我尝试了所有现有的 StackOverFlow 提示或解决方案,但没有成功。我不确定问题的根源是什么。我在 Mac 上使用 dotnet core 2.1.104。任何帮助将不胜感激。

.csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Common" Version="2.1.4" />
    <PackageReference Include="Microsoft.Azure.CosmosDB.Table" Version="1.1.2" />
    <PackageReference Include="Microsoft.Azure.DocumentDB" Version="1.22.0" />
    <PackageReference Include="Microsoft.WindowsAzure.ConfigurationManager" Version="3.2.3" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.Azure.Storage.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <HintPath>..\..\..\..\.nuget\packages\microsoft.azure.storage.common\9.0.0.1-preview\lib\netstandard1.3\Microsoft.Azure.Storage.Common.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

简单代码:

using Microsoft.Azure.CosmosDB.Table;
using Microsoft.Azure.Storage;

namespace Testing
{
    class Person : TableEntity
    {
        public string Firstname { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var connectionString =
                "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=1fgp8C2mImKcQfIFLMfAYBXwOK3LlYsXLyJdktuDEgXgmSCbJlDtd9tBeh2BgfnvGXmgltHFHzNnl7JpCR12Eg==;TableEndpoint=https://hello.table.cosmosdb.azure.com:443/;";

            // Retrieve the storage account from the connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "people" table.
            CloudTable table = tableClient.GetTableReference("people");

            // Create a new customer entity.
            Person customer1 = new Person {Firstname = "Walter@contoso.com"};

            // Create the TableOperation object that inserts the customer entity.
            TableOperation insertOperation = TableOperation.Insert(customer1);

            // Execute the insert operation.
            table.Execute(insertOperation);
        }
    }
}

最佳答案

根据我的测试,我认为您应该卸载所有关于 Microsoft.Azure.CosmosDB.Table 的包。

改为使用 WindowsAzure.Storage 包在表中添加实体。

此外,您还需要设置分区键和行键。 以下代码定义了一个实体类,它使用客户的名字作为行键,使用姓氏作为分区键。

An entity's partition and row key uniquely identify it in the table. Entities with the same partition key can be queried faster than entities with different partition keys, but using diverse partition keys allows for greater scalability of parallel operations.

可以引用下面的代码:

在程序中:

public static void  Main(string[] args)
        {
            method().Wait();
        }
        static private async Task method()
        {
            var connectionString = "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=xFWWad+YMoW/R7P54ppqGMDs7obGYj3ciEjokt+nkomwYfOh6mUcmcvJLV/puGistsKuGCfOwreCfptK1AwAAQ==;EndpointSuffix=core.windows.net";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("table1");

            Person customer1 = new Person("Harp", "Walter");
            customer1.Firstname = "Walter@contoso.com";
            TableOperation insertOperation = TableOperation.Insert(customer1);

            await table.ExecuteAsync(insertOperation);
        }
        class Person : TableEntity
        {
            public Person(string lastName, string firstName)
            {
                this.PartitionKey = lastName;
                this.RowKey = firstName;
            }

            public Person() { }

            public string Firstname { get; set; }
        }

在 csproj 中:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="WindowsAzure.Storage" Version="9.1.1" />
  </ItemGroup>

</Project>

关于c# - 无法加载文件或程序集 'Microsoft.Azure.Documents.Client - Azure-Table-Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223799/

相关文章:

c# - 在C#中将xml数据解析成一个数组

c# - 一次循环遍历 2 个列表

c# - ASP.Net Core Content-Disposition 附件/内联

asp.net-core - 如何解决 Blazor 中重新渲染缓慢的问题;如何获取各个组件的渲染时间?

c# - 在 C# 中使用十六进制文字将整数设置为负值

c# - C#Nest-ElasticSearch

c# - 在 docker 容器中包含 Entity Framework 工具

c# - 在 C# 中填充 Azure 表存储表

azure-functions - 在更新 Azure 存储帐户表时触发事件

java - Azure容器实例(Windows)是否支持访问表存储