10-Day .Net Aspire Challenge: Day 10?—?Azure Table Storage

10-Day .Net Aspire Challenge: Day 10?—?Azure Table Storage

Step-by-step guide on how to use the?.Net Aspire Azure Table Storage component in Visual?Studio.

Introduction

.Net Aspire framework is used to develop cloud and production-ready distributed applications. It consists of components to handle cloud-native concerns such as Redis, Postgres etc.

Prerequisites

10 Day?.Net Aspire Challenge Edit descriptionsinghsukhpinder.medium.com

Objectives

Learn how to create a starter project using?.Net Aspire with the Azure Table Storage.

Github Sample: The solution structure is divided into the following projects

  • DotnetAspireChallenge.ApiService
  • DotnetAspireChallenge.AppHost
  • DotnetAspireChallenge.ServiceDefaults
  • DotnetAspireChallenge.Web

Getting Started

Step 1: Install the following NuGet?package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.AppHost

dotnet add package Aspire.Hosting.Azure.Storage        

In the above project, register the Azure storage, table and emulator.

var storage = builder.AddAzureStorage("storage");
var tables = storage.AddTables("tables");        

Note: An Azure Table Storage connection string is required.

Step 2: Install another NuGet?package

Install the following Nuget package into the subsequent project “DotnetAspireChallenge.ApiService

dotnet add package Aspire.Azure.Data.Tables        

then register the context into the Program.cs file as follows

builder.AddAzureTableClient("tables");        

Step 3: Create an extension class

Create an extension class and register a minimal API send and receive method to demonstrate the QueueServiceClient usage in the API Service

public static class AspireAzureTableExtension
{
    public static void MapAzureTableStorageEndpoint(this WebApplication app)
    {
        app.MapPost("/create-table", async (TableServiceClient tableServiceClient) =>
        {
            string tableName = "MyTable";
            try
            {
                TableClient tableClient = tableServiceClient.GetTableClient(tableName);
                await tableClient.CreateIfNotExistsAsync();

                return Results.Ok($"Table '{tableName}' created successfully.");

            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("HTTP error code {0}: {1}", e.Status, e.ErrorCode);
                Console.WriteLine(e.Message);
                return Results.Problem($"HTTP error code {e.Status}: {e.Message}");
            }

            return Results.NotFound("Table creation failed or it does not exist.");
        });
    }
}        

and finally, register in the Program.cs file

app.MapAzureTableStorageEndpoint();        

Add additional connection string properties using the JSON syntax

{
  "Aspire":{
    "Azure": {
      "Data": {
        "Tables": {
          "ServiceUri": "YOUR_URI",
          "DisableHealthChecks": true,
          "DisableTracing": false,
          "ClientOptions": {
          "EnableTenantDiscovery": true
          }
        }
      }
    }
  }
}        

Congratulations..!! You’ve successfully integrated the Azure Table Storage component into the?.Net Aspire project.

Github Project

GitHub?—?ssukhpinder/DotnetAspireChallenge: 10 Day?.Net Aspire Challenge

More Cheatsheets & Best Practices

Cheat Sheets?—?.Net singhsukhpinder.medium.com

30 Day?.Net Challenge singhsukhpinder.medium.com

Best Practices singhsukhpinder.medium.com

Design Patterns?—?.Net singhsukhpinder.medium.com

C# Programming??

Thank you for being a part of the C# community! Before you leave:

Follow us: Youtube | X | LinkedIn | Dev.to

Visit our other platforms: GitHub

More content at C# Programming


要查看或添加评论,请登录

社区洞察

其他会员也浏览了