Using Azure Storage? Use the new Nuget Packages or for Tables Microsoft.Azure.CosmosDb.Table

by Patrick Lee on 10 Apr 2020 in categories tech with tags ASP.NET Core AzureTables

Microsoft has recently released new versions of Nuget Packages to enable developers to read and write to Azure Storage accounts,

For example, to connect to blobs (binary large objects, typically images or files), queues (for distributed processing or to enable web apps to scale by decoupling front end from time consuming backend processing), or tables (for unstructured data storage).  

For blobs, queues and files

Use the new Nuget packages: Azure.Storage.Blobs (e.g. I have just used version 12.4.1), Azure.Storage.Queues and Azure.Storage.Files.Shares respectively.

For Tables

Azure Storage Tables provide a cheaper (but less powerful) way of storing relatively simple unstructured data as opposed to the more powerful (and expensive) facility in Azure CosmosDb.  

For some reason, there isn't an equivalent package for Azure Storage Tables. Instead, rather than using the previous WindowsAzure.Storage (version 9.3.3) Nuget package, you can use the Microsoft.Azure.CosmosTable Nuget package to connect to Azure Storage Tables.

Example of using Microsoft.Azure.Cosmos.Table to connect to an Azure Storage Table

I used version 2.0.0 preview (version 1.0.6 also works) of the package, but an earlier version may also work, I haven't tried this, (There doesn't seem to be any online documentation specifically for this, only for connecting to a CosmosDb table, but similar code seems to work for an Azure Storage Table).

The code below was for an ASP.NET Core 3.1 api web app:

For an Azure storage Table called MyTable, with a corresponding class called MyTableEntity:

using Microsoft.Azure.Cosmos.Table; // added for TableEntity

    public class MyTableEntity: TableEntity

    {

        public MyTableEntity(string type, string id)

        {

            this.PartitionKey = type;

            this.RowKey = id;

        }

        public MyTableEntity() { }

// put your table's other properties here

    }

then in a controller:

    using Microsoft.Azure.Cosmos.Table;

      private async Task<List<MyTableEntity>> GetMyTableEntities()

        {

            var myTable = GetMyTable();

            // Construct the query operation for all entities where PartitionKey="putpartitionkeyvaluehere";

            TableQuery<MyTableEntity> query =

                new TableQuery<MyTableEntity>().Where(

                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "putpartitionkeyvaluehere"));

 

            var records = new List<MyTableEntity>();

            TableContinuationToken token = null;

            do

            {

                TableQuerySegment<MyTableEntity> resultSegment = await myTable .ExecuteQuerySegmentedAsync(query, token);

                token = resultSegment.ContinuationToken;

                records .AddRange((resultSegment.Results));

            } while (token != null);

            return records ;

        }

 

        private CloudTable GetMyTable()

        {   // use dependency injection to set _config (the configuration) in the controller's constructor or use KeyVault or ManagedIdentity to avoid having to store secrets in settings

            var connectionString = _config.GetValue<string>("AzureStorage:ConnectionString"); 

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            CloudTable myTable = tableClient.GetTableReference("MyTable");

            return myTable;

        }