0

I am a new azure table user, and I am trying to write something to a table could someone please explain me why the storageAccount.CreateTableClient() doesn't work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Azure.Storage.Queue;
using Microsoft.Azure.Storage;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;




namespace project_3_backend
{
    public class Functions
    {
        private static CloudTable LoanInfo = null;

        private static IConfiguration Configuration;

        public Functions(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public static void WritetoTable()
        {
            string connectionString = Configuration.GetValue<String>("AzureWebjobsStorage");
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateTableClient();
            CloudTable table_mortage = tableClient.GetTableReference("LoanInfo");
        }
}
  • _doesn't work_ isn't really helpful. What doesn't work exactly, do you get null, an exception. Please be more specific. – JSteward Mar 03 '20 at 20:47

2 Answers2

1

Table Services is now under Cosmos Db:

Install the following nuget packages:

  • Microsoft.Azure.Cosmos.Table
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.Configuration.Binder

https://learn.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-table-dotnet#install-the-required-nuget-package

Then:

public static async Task<CloudTable> CreateTableAsync(string tableName)
  {
    string storageConnectionString = AppSettings.LoadAppSettings().StorageConnectionString;

    // Retrieve storage account information from connection string.
    CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(storageConnectionString);

    // Create a table client for interacting with the table service
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());

    Console.WriteLine("Create a Table for the demo");

    // Create a table client for interacting with the table service 
    CloudTable table = tableClient.GetTableReference(tableName);
    if (await table.CreateIfNotExistsAsync())
    {
      Console.WriteLine("Created Table named: {0}", tableName);
    }
    else
    {
      Console.WriteLine("Table {0} already exists", tableName);
    }

    Console.WriteLine();
    return table;
}

Probably you can still use old nuget packages to make it work with Azure Storage, but I am afraid you won't have latest version of the packages.

https://learn.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-table-dotnet#parse-and-validate-the-connection-details

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

I believe you're getting this error is because you're using two different Storage SDKs in your code.

You're using Microsoft.WindowsAzure.Storage which is the older SDK as well as Microsoft.Azure.Storage which is the newer one. Currently you're getting a reference for CloudStorageAccount from Microsoft.Azure.Storage namespace and that does not have a definition for CloudTableClient and hence you're getting this error.

One solution is to use Microsoft.Azure.Cosmos.Table as mentioned in the other answer. Going forward this is the recommended way.

Other option is to just use the older SDK (Microsoft.WindowsAzure.Storage). In that case, you will get CloudStorageAccount from Microsoft.WindowsAzure.Storage namespace and that has a definition for CloudTableClient.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241