5

I am trying to use Azure Table Api with dotnet core and I keep getting this exception:

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

Apparently this is a common exception message. I put my simple code on GitHub just in case. I think I tried every existing StackOverFlow hint or solution but no success. I am not sure what is the source of my problem. I am using dotnet core 2.1.104 on Mac. Any help would be greatly appreciated.

.csproj file:

<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>

Simple code:

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);
        }
    }
}
Node.JS
  • 1,042
  • 6
  • 44
  • 114
  • 2
    Microsoft has released support for .NET Core with the new Microsoft.Azure.Cosmos.Table NuGet package. Use that instead of the Microsoft.Azure.CosmosDB.Table package. – Per Noalt Mar 28 '19 at 07:24

2 Answers2

5

As I have tested, I think you should uninstall all your package about Microsoft.Azure.CosmosDB.Table.

Change to use WindowsAzure.Storage package to add entity in table.

Also, you need to set the partition key and row key. The following code defines an entity class that uses the customer's first name as the row key and last name as the partition key.

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.

You could refer to the following code:

In Program:

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; }
        }

In 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>
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Thank you so much! Thank you. – Node.JS May 08 '18 at 06:06
  • PartitionKey = lastName; RowKey = firstName. What happens when you have two records by the name John Doe? – nmrlqa4 Jun 12 '18 at 10:36
  • **An entity's partition and row key uniquely identify it in the table.** You could have two different partition key with the same row key but you could not have one partition key with two same row key. – Joey Cai Jun 13 '18 at 01:23
  • I am not getting method "CreateCloudTableClient" once I remove the CosmosDB package. The WindowsAzure.Storage package is present and the only item it gives is the TableEndpoint and TableEndpointUri... Is there a versioning issue ? My version of WindowsAzure.Storage is 9.3.1 (Aug 2018 release) – NitinSingh Aug 13 '18 at 12:54
  • Reverting to version 9.1.1 fixed the issue. But since we need to be continually updating our references to stable builds, why is this dependency not getting resolved.. As per original question – NitinSingh Aug 13 '18 at 13:07
  • When you use `Microsoft.Azure.CosmosDB.Table` package, the connectionstring is not the storage account connectionstring, and it use tableapi. When you use 1WindowsAzure.Storage1 package, it may have the version control issue. – Joey Cai Aug 14 '18 at 02:42
5

Replace Microsoft.Azure.CosmosDB.Table package with Microsoft.Azure.Cosmos.Table. Replace Microsoft.Azure.CosmosDB.Table namespace with Microsoft.Azure.Cosmos.Table.

Der_Meister
  • 4,771
  • 2
  • 46
  • 53
  • 2
    This is good, except when you are also using `Microsoft.Azure.Storage.CloudStorageAccount` for blob storage because it will create an ambiguous reference between itself and `Microsoft.Azure.CosmosDB.Table.CloudStorageAccount`. This is easily taken care of by forcing unambiguity by writing the namespace before object declarations. – craigbot Jul 31 '19 at 14:46