The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage

The.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. The solution structure is divided into the following projects: DotnetAspireChallenge.ApiService, Dotnet aspire.AppHost and Dotnet Aspire.Web.


This content originally appeared on HackerNoon and was authored by Sukhpinder Singh

Step-by-step guide on how to use the .Net Aspire Azure Queue 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

Objectives

Learn how to create a starter project using .Net Aspire with the Azure Queue 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, queue and emulator.

    var storage = builder.AddAzureStorage("storage");
    var queues = storage.AddQueues("queues");

**Note: ** The particular tag is used to skip version checks.

Step 2: Install another NuGet package

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

dotnet add package Aspire.Azure.Storage.Queues

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

    builder.AddAzureQueueClient("queues");

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 AspireAzureQueueExtension
    {
        public static void MapAzureQueueEndpoint(this WebApplication app)
        {
            app.MapGet("/queue-send", async (QueueServiceClient queueServiceClient) =>
            {
                try
                {
                    var queueClient = queueServiceClient.GetQueueClient("test");
                    await queueClient.CreateIfNotExistsAsync();

                    if (await queueClient.ExistsAsync())
                    {
                        await queueClient.SendMessageAsync("Test Message ");
                        return Results.Ok($"Message sent to queue: test");
                    }
                    return Results.NotFound($"Queue not found: test");
                }
                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}");
                }
            });


            app.MapGet("/queue-recieve", async (QueueServiceClient queueServiceClient) =>
            {
                try
                {
                    var queueClient = queueServiceClient.GetQueueClient("test");
                    if (await queueClient.ExistsAsync())
                    {
                        var response = await queueClient.ReceiveMessageAsync();
                        if (response?.Value != null)
                        {
                            var message = response.Value;
                            // Delete the message after processing
                            await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
                            return Results.Ok($"Received message: {message.MessageText}");
                        }
                        return Results.Ok("No messages in the queue.");
                    }
                    return Results.NotFound($"Queue not found: test");
                }
                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}");
                }
            });
        }
    }

and finally, register in the Program.cs file

    app.MapAzureQueueEndpoint();

Add additional connection string properties using the JSON syntax

    {
      "Aspire": {
        "Azure": {
          "Storage": {
            "Queues": {
              "DisableHealthChecks": true,
              "DisableTracing": false,
              "ClientOptions": {
                "Diagnostics": {
                  "ApplicationId": "myapp"
                }
              }
            }
          }
        }
      }
    }

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

Github Project

GitHub - ssukhpinder/DotnetAspireChallenge: 10 Day .Net Aspire Challenge

More Cheatsheets

Cheat Sheets — .Net

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


This content originally appeared on HackerNoon and was authored by Sukhpinder Singh


Print Share Comment Cite Upload Translate Updates
APA

Sukhpinder Singh | Sciencx (2024-09-06T19:50:48+00:00) The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage. Retrieved from https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/

MLA
" » The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage." Sukhpinder Singh | Sciencx - Friday September 6, 2024, https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/
HARVARD
Sukhpinder Singh | Sciencx Friday September 6, 2024 » The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage., viewed ,<https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/>
VANCOUVER
Sukhpinder Singh | Sciencx - » The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/
CHICAGO
" » The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage." Sukhpinder Singh | Sciencx - Accessed . https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/
IEEE
" » The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage." Sukhpinder Singh | Sciencx [Online]. Available: https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/. [Accessed: ]
rf:citation
» The 10-Day .Net Aspire Challenge – Day 8: Azure Queue Storage | Sukhpinder Singh | Sciencx | https://www.scien.cx/2024/09/06/the-10-day-net-aspire-challenge-day-8-azure-queue-storage/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.