Tuesday, 3 April 2018

How to add message in azure queue using C#

Description : In this post how to add message in azure queue storage using c# code. This code use in any .Net application like Winform application or MVC or ASP.Net. below steps for how to successfully add message in azure queue using c#

Step 1 : Downlod below NuGet package for add message using c#. Install both package one by one in your project use NuGet console or NuGet package managerr

- WindowsAzure.Storage
- WindowsAzure.ConfigurationManager


Step 2 : Get your connection string from Azure. Go to Azure Portal
Go to Storage Account -> Click on your storage account -> Inside Setting Click on Access keys -> In this window select Copy connection string. your connection string look like below

- DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey

Step 3 : Create class for add json message in azure queue using c#

public class RequestClass
{
    public string id { get; set; }
    public string name { get; set; }

    public RequestClass() { }

    public RequestClass(string _id, string _name)
    {
        this.id = _id; this.name = _name;
    }
}

Step 4 : Write below code for add message in your queue using C#.

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey"

CloudStorageAccount account = CloudStorageAccount.Parse(ConnectionString); // Cloud Storage Account

CloudQueueClient queueClient = account.CreateCloudQueueClient(); // Queue Client Create

CloudQueue messageQueue = queueClient.GetQueueReference("YourAzureQueueName"); // Get Queue Reference

string MessageToAdd = JsonConvert.SerializeObject(new RequestClass(GUID, Name)); // Serialize JSON for add in queue

CloudQueueMessage m = new CloudQueueMessage(MessageToAdd); // Generate Cloud Message

messageQueue.AddMessage(m); // Finally add message in queue

No comments:

Post a Comment