Author : MD TAREQ HASSAN | Updated : 2020/10/23
Prerequisites
Dependency
The following package works for both ‘Azure table storage’ and ‘Azure CosmosBD Table API’
Install-Package Microsoft.Azure.Cosmos.Table
(old package: Microsoft.Azure.CosmosDB.Table
=> should be upgraded)
Setup
appsettings.json
(best way: put connection string in Azure KeyVault, see: Using Azure KeyVault in Production)
{
"az_storage_account_connection_string": "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net",
"az_table_storage_name": "ordertable",
}
Constants.cs
public static class Constants
{
public static readonly string KEY_AZURE_STORAGE_ACCOUNT_CONNECTION_STRING = "az_storage_account_connection_string";
public static readonly string KEY_TABLE_NAME = "az_table_storage_name";
public static readonly string KEY_TABLE_PARTITION_NAME = "az_table_partition_name";
}
OrderHistoryItem.cs
using Microsoft.Azure.Cosmos.Table;
namespace Foo.Models
{
public class OrderHistoryItem : TableEntity
{
public string Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Size { get; set; }
}
}
ITableService.cs
public interface ITableService
{
Task<List<OrderHistoryItem>> GetOrderHistoryAsync();
}
AzureTableService.cs
using Foo.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Azure.Cosmos.Table;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Foo.Services
{
public class AzureTableService : ITableService
{
string azureStorageAccountConnectionString;
string tableName;
string partitionName;
public AzureTableService(IConfiguration configuration)
{
azureStorageAccountConnectionString = configuration[Constants.KEY_AZURE_STORAGE_ACCOUNT_CONNECTION_STRING];
tableName = configuration[Constants.KEY_TABLE_NAME];
partitionName = configuration[Constants.KEY_TABLE_PARTITION_NAME];
}
public async Task<List<OrderHistoryItem>> GetOrderHistoryAsync()
{
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString: azureStorageAccountConnectionString);
var tableClient = cloudStorageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableName);
// await table.CreateIfNotExistsAsync();
var historyQuery = new TableQuery<Models.OrderHistoryItem>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionName));
TableContinuationToken queryToken = null;
var tableItems = await table.ExecuteQuerySegmentedAsync<OrderHistoryItem>(query: historyQuery, token: queryToken);
return tableItems.ToList();
}
}
}
Read data from table storage
Scenario: API controller fetches list of oders from Azure Table Storage and return as json
using Foo.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Foo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
ITableService tableService;
public OrderController(ITableService tableService)
{
this.tableService = tableService;
}
[HttpGet]
public async Task<IActionResult> GetOrderHistory()
{
var items = await tableService.GetOrderHistoryAsync();
return new JsonResult(items);
}
}
}
More operations
See: