06 September 2023

What is Azure Storage Queue and how to Create Azure Storage Queue message using C# console application.

Azure Storage Queue is a component of Azure Storage Account which is used for messaging service. 
We can Add message to a Queue and also dequeue (Delete) the message. When dequeue a message then the dequeue count will get increased.
Generally adding and queue message and deleting a queue message has been done by programmatically. So, my intention is to demonstrate a simple CRUD operation on queue message using C# console app.
Step 1: Create a C# console app. (I will use Visual Studio 2022).
Step 2: Use NuGet Package Manager and download “Azure.Storage.Queues”.
Step 3: Get the connection string first. To get the connection details 1st we have to go to the storage account and then Access key section. Remember the connection string is a part of azure storage account.
Get the queue name which you have just created. In my case the queue name is “lazylearnerqueue”.
Step 4: Now we are ready with our console app and we can start the Create operation.
  • We need to create a connection using the above mention connection string and queue name.
  • Add “using Azure.Storage.Queues;” reference to the code.
  • Use “QueueClient” to create the connection.


Following is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Queues;
namespace AzureStorageQueueCRUDApp
{
    internal class AzureStorageQueueCRUDApp
    {
        static void Main(string[] args)
        {
            //Get the connection string from azure storage account Access Key
            string AzureStorageQueueConnection ="*************";
            string QueueName = "lazylearnerqueue";
            createQueueMessage("Test Message 1", QueueName, AzureStorageQueueConnection);
        }
        static void createQueueMessage(string queueMessageBody, string QueueName, string AzureStorageQueueConnection)
        {
            //Create a connection. 

            QueueClient queueClient = new QueueClient(AzureStorageQueueConnection, QueueName);
            if(queueClient.Exists())
            {
                queueClient.SendMessage(queueMessageBody);
                Console.WriteLine(queueMessageBody);
            }
        }
    }
}

We create a .Net object as a Message:

  • We need to transfer a .net object to a JSON string and then we can send it to the Queue.
  • We can create a class in our project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Storage.Queues;
namespace AzureStorageQueueCRUDApp
{


public class ModelObject
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Type { get; set; }
}


internal class AzureStorageQueueCRUDApp
    {
        static void Main(string[] args)
        {
            //Get the connection string from azure storage account Access Key
            string AzureStorageQueueConnection = "***********";
            string QueueName = "lazylearnerqueue";
            createQueueMessageUsingObject(new ModelObject() { Description = "Description Detail",Name="Name details",Type = "Type Details"}, QueueName, AzureStorageQueueConnection);
        }

        static void createQueueMessageUsingObject(ModelObject queueMessageBody, string QueueName, string AzureStorageQueueConnection)
        {
            //Create a connection. 
            QueueClient queueClient = new QueueClient(AzureStorageQueueConnection, QueueName);
            if (queueClient.Exists())
            {
                var SerializeModelObject = JsonSerializer.Serialize(queueMessageBody);
                queueClient.SendMessage(SerializeModelObject);
                Console.WriteLine("Message is created");
                Console.ReadLine();
            }
        }
    }
}

 


17 August 2023

What is “Azure messaging service”? When and why, we can use this service?

"Azure messaging service" is a platform where one component can send messages and another component can receive those messages from that platform for processing. It can be used during integration with Microsoft Dynamics CE and third parties.


In the above figure we can see that an MVC application stores unprocessed data in a database and then fetches the location and name of the data from that database and finally sends this information to the Azure Messaging Service as a message (JSON) where each message object contains the name and location of the data. Whenever a message arrives in Azure Messaging Service, an application retrieves and locks the message so that no other application can retrieve the same message and finally processes it and stores it in the database.

Now the question is why not sending messages directly to the application instead of Azure Messaging Service?

At first glance it may seem that the use of Azure Messaging Service is pointless here, but if you look a little closer, you will see that there is a reason for this.
  1. When a message is sent, they will fight among themselves over which of the three applications will process it.
  2. If an application fails to process then the message will be lost or the earlier mentioned MVC application has to resend the same message which means the MVC application has to wait for successful completion of processing the message from the process application.
Why Send through Azure Messaging Service?
  1. If an application fails to process then the message will come to the Azure Messaging Service again and other application can pick it.
  2. The MVC applications no longer have to wait. Which means a decoupling architecture can be created.
We can divide this messaging service into two parts.
  1. Azure Storage Queues.
  2. Azure Service Bus.

15 August 2023

How to create Azure Key Vault and how to get secret key using a C# console application from local computer?

This tutorial is for beginners and especially for those who have no idea about Azure Key Vault.

In this tutorial I will explain how to create an Azure Key Vault and how we can fetch Azure Key Vault data on local computer using C# console application.

The minimum requirement is to follow the steps by yourself
  1. Need an Azure Account.
  2. Visual Studio / Visual Studio Code.
  3. C# Knowledge (Beginner level).
Step 1:  Log in to azure account and Search for Azure Key Vault and Create a new Key Vault.
Select Vault access policy true.
Step 2: Create a secret by clicking on the + Generate/Import button.
Set activation and expiration date is an optional, enter secret name and secret value.
Step 3: Now go to Azure AD and create an app registration and in the api permission section add azure key vault permission and then add a secret (Keep the secrect value with you because it will only show at the time of creation).
Step 4: Now go back to the Key Vault (“MSDynamicCESecretKey”) again and create access policy and set secret permissions as Get in the Azure Key Vault and select the principal.
Step 5: Create a c# console application.

using System;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
class Program
{
    static void Main(string[] args)
    {
        string keyVaultUrl = "https://msdynamiccesecretkey.vault.azure.net/";
        string secretName = "SecretKey";

        //Get TenantId, ClientId and  ClientSecret From AD App registration
        string tenantId = "a8eaa793-754b-4d21-9e60-a882c97f7bab";
        string clientId = "209b0d23-0f28-452a-b971-4e316bc3f5b8";
        string clientSecret = "***************************";

        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
        var secretClient = new SecretClient(new Uri(keyVaultUrl), clientSecretCredential);
        
        KeyVaultSecret secret = secretClient.GetSecret(secretName);

        Console.WriteLine($"The key vault Secret value is : {secret.Value}");
        Console.ReadLine();
    }
}

// The code mentioned above is the simplest code that only shows how to get the secret value from the Azure Key vault.

What is Azure Storage Queue and how to Create Azure Storage Queue message using C# console application.

Azure Storage Queue is a component of Azure Storage Account which is used for messaging service.  We can Add message to a Queue and also deq...