Problem
How to use Azure Table Storage in ASP.NET Core.
Solution
Create a class library and add NuGet package: WindowsAzure.Storage
.
Add a class to encapsulate settings:
public class AzureTableSettings
{
public AzureTableSettings(string storageAccount,
string storageKey,
string tableName)
{
if (string.IsNullOrEmpty(storageAccount))
throw new ArgumentNullException("StorageAccount");
if (string.IsNullOrEmpty(storageKey))
throw new ArgumentNullException("StorageKey");
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("TableName");
this.StorageAccount = storageAccount;
this.StorageKey = storageKey;
this.TableName = tableName;
}
public string StorageAccount { get; }
public string StorageKey { get; }
public string TableName { get; }
}
Add a class to encapsulate storage access. Add a private
helper method to access storage:
private async Task<CloudTable> GetTableAsync()
{
CloudStorageAccount storageAccount = new CloudStorageAccount(
new StorageCredentials(this.settings.StorageAccount, this.settings.StorageKey), false);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(this.settings.TableName);
await table.CreateIfNotExistsAsync();
return table;
}
Now add public
methods for the storage:
public async Task<List<T>> GetList()
{
CloudTable table = await GetTableAsync();
TableQuery<T> query = new TableQuery<T>();
List<T> results = new List<T>();
TableContinuationToken continuationToken = null;
do
{
TableQuerySegment<T> queryResults =
await table.ExecuteQuerySegmentedAsync(query, continuationToken);
continuationToken = queryResults.ContinuationToken;
results.AddRange(queryResults.Results);
} while (continuationToken != null);
return results;
}
public async Task<List<T>> GetList(string partitionKey)
{
CloudTable table = await GetTableAsync();
TableQuery<T> query = new TableQuery<T>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey));
List<T> results = new List<T>();
TableContinuationToken continuationToken = null;
do
{
TableQuerySegment<T> queryResults =
await table.ExecuteQuerySegmentedAsync(query, continuationToken);
continuationToken = queryResults.ContinuationToken;
results.AddRange(queryResults.Results);
} while (continuationToken != null);
return results;
}
public async Task<T> GetItem(string partitionKey, string rowKey)
{
CloudTable table = await GetTableAsync();
TableOperation operation = TableOperation.Retrieve<T>(partitionKey, rowKey);
TableResult result = await table.ExecuteAsync(operation);
return (T)(dynamic)result.Result;
}
public async Task Insert(T item)
{
CloudTable table = await GetTableAsync();
TableOperation operation = TableOperation.Insert(item);
await table.ExecuteAsync(operation);
}
public async Task Update(T item)
{
CloudTable table = await GetTableAsync();
TableOperation operation = TableOperation.InsertOrReplace(item);
await table.ExecuteAsync(operation);
}
public async Task Delete(string partitionKey, string rowKey)
{
T item = await GetItem(partitionKey, rowKey);
CloudTable table = await GetTableAsync();
TableOperation operation = TableOperation.Delete(item);
await table.ExecuteAsync(operation);
}
Inject and use storage:
private readonly IAzureTableStorage<Movie> repository;
public MovieService(IAzureTableStorage<Movie> repository)
{
this.repository = repository;
}
In ASP.NET Core Web Application, configure services:
public void ConfigureServices(
IServiceCollection services)
{
services.AddScoped<IAzureTableStorage<Movie>>(factory =>
{
return new AzureTableStorage<Movie>(
new AzureTableSettings(
storageAccount: Configuration["Table_StorageAccount"],
storageKey: Configuration["Table_StorageKey"],
tableName: Configuration["Table_TableName"]));
});
services.AddScoped<IMovieService, MovieService>();
services.AddMvc();
}
Discussion
The sample code will require you to setup Azure account, storage account and table. Instructions for these could be found here.