Introduction
In this post, we will create a single page application in Blazor. We will store the data in Azure Table Storage. Also, we will see all the CRUD operations in this Blazor project. For that, we will create a Person app by storing the Id, Name, Country, Age, and Gender. We will generate the Id column value as GUID.
About Azure Table Storage
Azure Table Storage is a service that stores the structured NoSQL data in the cloud, providing a key/attribute store with a schemaless design. Table storage is always schema-less. Access to Table Storage data is fast and cost-effective for many types of applications and it is typically lower in cost than traditional SQL for similar volumes of data.
You can use table storage to store the flexible datasets like user data for web applications, device information, address books, or any other types of metadata that your service requires. You can store any number of entities in a table, and a storage account may contain any number of tables, up to the capacity limit of your storage account.
The “PartitionKey” and “RowKey” are major elements in Azure Table Storage and are used to save the data in a structured way, like indexing in SQL. “RowKey” mainly acts as a primary key in SQL.
You can refer to the below article to get more details on Azure Table Storage.
About Blazor Framework
Blazor is (still an experimental) .NET web framework from Microsoft using C#/Razor and HTML that runs in the browser with Web Assembly. Blazor provides all the benefits of a client-side web UI framework using .NET on the client and optionally on the server.
I have already written many articles on Blazor for you good people on C# Corner. If you are new to it, please refer to the below articles to get started with Blazor.
- Single Page Application With Blazor And CosmosDB
- Blazor - CRUD Using PostgreSQL And Entity Framework Core
- Blazor - Connect With Amazon DynamoDB Using AWS SDK
- Blazor - Work With Cassandra API In Cosmos DB
- Localization In Blazor App Using Microsoft.JSInterop
- Blazor - Create SPA With Azure Database For MariaDB Server
- Blazor - Connect With Oracle Database In Amazon RDS
- Get C# Corner RSS Feeds In Blazor Project
- C# Corner RSS Feeds In Blazor With Pagination
- Deploy Blazor Application On AWS Cloud Using Elastic Beanstalk
- Azure Redis Cache With Azure SQL In Blazor Project
Create Azure Table Storage
Log into Azure Portal.
Create a Resource -> choose Storage Account.
After choosing the resource group, you can give a valid and unique (globally) name to the storage account. Click the “Review + create” button to validate the service details and then click the “Create” button to start the deployment of the storage account.
After a few moments, our storage account service will be ready.
Go to the Resource and choose “Configuration” blade and select “Disabled” option under "Secure transfer required". Click the “Save” button to save the settings. If you do not choose this, you can’t connect Azure Table Storage to Blazor application without HTTPS.
Go to “Overview” blade and choose “Tables” option to create a new table.
We can add multiple tables to the same storage account depending on the capacity of the storage account. Each table can contain multiple entities and each entity can store multiple documents. We will create a “Person” entity using C# in our Blazor project later.
You can click the “Access keys” blade and copy the connection string and save that to a secure place. We will use this connection string later in our Blazor application.
Create a Blazor project in Visual Studio 2017
We can create a new Blazor project using Visual Studio 2017 (I am using free community edition). Currently, there are three types of templates available for Blazor. We can choose Blazor (ASP.NET Core hosted) template.
Our solution will be ready shortly. Please note that there are three projects created in our solution - “Client”, “Server”, and “Shared”.
By default, Blazor creates many files in these three projects. We can remove all the unwanted files like “Counter.cshtml”, “FetchData.cshtml”, “SurveyPrompt.cshtml” from the Client project, “SampleDataController.cs” file from the Server project, and remove “WeatherForecast.cs” file from the Shared project.
We are going to process Azure Table Storage operations using “WindowsAzure.Storage” NuGet package. We must install this package in “Shared” project.
Create a “Models” folder in “Shared” project and create a “Person” class inside this folder.
Person.cs
using Microsoft.WindowsAzure.Storage.Table;
namespace BlazorAzureTableStorage.Shared.Models {
public class Person: TableEntity {
public string Id {
get;
set;
}
public string Name {
get;
set;
}
public string Country {
get;
set;
}
public int Age {
get;
set;
}
public string Gender {
get;
set;
}
}
}
Please note that we have inherited this class from the “TableEntity” class. We will use the “PartitionKey” and “RowKey” properties from the TableEntity class.
We can create a “DataAccess” folder in the Shared project. We then create “AzureTableSettings” inside this folder.
AzureTableSettings.cs
namespace BlazorAzureTableStorage.Shared.DataAccess {
public class AzureTableSettings {
public AzureTableSettings(string connectionString, string tableName) {
TableName = tableName;
ConnectionString = connectionString;
}
public string TableName {
get;
}
public string ConnectionString {
get;
set;
}
}
}
This file contains the ConnectionString and table name properties which we will later use to establish the CRUD operations.
Create an “IAzureTableStorage” interface to define the method declaration for CRUD operations.
IAzureTableStorage.cs
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BlazorAzureTableStorage.Shared.DataAccess {
public interface IAzureTableStorage < T > where T: TableEntity, new() {
Task Delete(string rowKey);
Task < T > GetItem(string rowKey);
Task < List < T >> GetList();
Task Insert(T item);
Task Update(T item);
}
}
We can now implement the above interface in “AzureTableStorage” class.
AzureTableStorage.cs
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BlazorAzureTableStorage.Shared.DataAccess {
public class AzureTableStorage < T > : IAzureTableStorage < T > where T: TableEntity, new() {
private readonly AzureTableSettings settings;
public AzureTableStorage(AzureTableSettings settings) {
this.settings = settings;
}
public async Task < List < T >> GetList() {
//Table
CloudTable table = await GetTableAsync();
//Query
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 < T > GetItem(string rowKey) {
//Table
CloudTable table = await GetTableAsync();
//Operation
TableOperation operation = TableOperation.Retrieve < T > ("", rowKey);
//Execute
TableResult result = await table.ExecuteAsync(operation);
return (T)(dynamic) result.Result;
}
public async Task Insert(T item) {
//Table
CloudTable table = await GetTableAsync();
//Operation
TableOperation operation = TableOperation.Insert(item);
//Execute
await table.ExecuteAsync(operation);
}
public async Task Update(T item) {
//Table
CloudTable table = await GetTableAsync();
//Operation
TableOperation operation = TableOperation.InsertOrReplace(item);
//Execute
await table.ExecuteAsync(operation);
}
public async Task Delete(string rowKey) {
//Item
T item = await GetItem(rowKey);
//Table
CloudTable table = await GetTableAsync();
//Operation
TableOperation operation = TableOperation.Delete(item);
//Execute
await table.ExecuteAsync(operation);
}
private async Task < CloudTable > GetTableAsync() {
//Account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(this.settings.ConnectionString);
//Client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
//Table
CloudTable table = tableClient.GetTableReference(this.settings.TableName);
await table.CreateIfNotExistsAsync();
return table;
}
}
}
We can create another interface “IPersonService” for the Person service. It will contain all the method declarations in the previous interface IAzureTableStorage. Only the method names are changed.
IPersonService.cs
using BlazorAzureTableStorage.Shared.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BlazorAzureTableStorage.Shared.DataAccess {
public interface IPersonService {
Task AddPerson(Person item);
Task DeletePerson(string id);
Task < Person > GetPerson(string id);
Task < List < Person >> GetPeople();
Task UpdatePerson(Person item);
}
}
Create a service class “PersonService” and implement the interface IPersonService.
PersonService.cs

Ankur MistryPosted Dec 26, 2018, 2:46 AM
Thanks for Sharing Sarathlal Saseendran