Azure Cloud
Azure Azure Storage Azure Storage Queue C#

How to Queue Requests through an Azure Storage Queue

Welcome to today’s post.

In today’s post I will be showing you can post messages to an azure storage queue using the Azure Storage Queue API using.NET Core. I will then show how to add structured data to the storage queue. Adding structured data to the message queue allows us to provide more meaningful data to a consumer of our queue message. The consumer, an Azure function can then construct an orchestration or workflow from the message item.

The other use for an Azure Storage Queue is to enable a client web application or a web API service to be decoupled from an external Web Job that is used to run background tasks. The jobs would be triggered by new or modified data that is enqueued into the storage queue.

The other reason is to remove server load from the application by making the queue posting asynchronous so that the web application or API is responsive to end users.

Azure Storage Queue

To develop Azure Storage Queues in .NET Core applications we will need to install the NuGet package:

Azure.Storage.Queues

I will go through the process of submitting messages to an Azure storage queue.

We can submit messages through different sources including a web application or web API method. I will show how this is done using an existing API method.

In this case we are posting a new record into a review table, keeping the isVisible flag to false.

We can do this using POSTMAN or through an API service using the Swagger UI.

An abbreviated form for our class that will contain our payload data is shown:

public class ReviewViewModel
{
    public int ID { get; set; }
    public string Reviewer { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public string Heading { get; set; }
    public string Comment { get; set; }
    public int Rating { get; set; }
    public int BookID { get; set; }
    public bool IsVisible { get; set; }
    public string Approver { get; set; }
	..
}

Once posted the data is shown. We will then proceed to post some data to our storage queue.

Our review service method to insert new data and call our storage queue service is shown:

public async Task SaveReview(ReviewViewModel vm)
{
    ReviewViewModel reviewViewModel = new ReviewViewModel()
    {
        BookID = vm.BookID,
        Heading = vm.Heading,
        Comment = vm.Comment,
        Rating = vm.Rating,
        DateReviewed = vm.DateReviewed,
        DateCreated = DateTime.Now,
        Reviewer = vm.Reviewer,
        IsVisible = false,
        Approver = ""
    };
    _db.Add(reviewViewModel);
    await _db.SaveChangesAsync();
    _azureStorageQueueService.InsertMessage(reviewViewModel);
}

Before we can push messages into our queue, we will need to either create or use an existing queue.

We will also need to declare a variable to hold our queue client instance:

private QueueClient _queueClient;

A method to create or use an existing queue is shown:

public bool CreateQueue()
{
    try
    {
        string connectionString = _appConfiguration.Value.StorageConnectionString;

        _queueClient = new QueueClient(connectionString, _queueName);

        _queueClient.CreateIfNotExists();

        if (_queueClient.Exists())
        {
            Console.WriteLine($"Queue created: '{_queueClient.Name}'");
            return true;
        }
        else
        {
            Console.WriteLine($"Make sure the Azure storage emulator is running and try again.");
            return false;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.Message}\n\n");
        Console.WriteLine($"Make sure the Azure storage emulator is running and try again.");
        return false;
    }
}

Our method to push the message into our queue in a structured format is shown below:

public void InsertMessage(ReviewViewModel message)
{
    if (!this.CreateQueue())
        return;

    if (_queueClient.Exists())
    {
        var payload = new {
            ID = message.ID.ToString(),
            BookID = message.BookID.ToString(),
            Approver = message.Reviewer.ToString(),
            Rating = message.Rating.ToString(),
            Comment = message.Comment.ToString()
        };

        var queuedMessage = JsonConvert.SerializeObject(payload);

        _queueClient.SendMessage(queuedMessage);
    }

    Console.WriteLine($"Inserted: {message}");
}

Before we can push the data across to the queue, we will deserialize the data into JSON format:

We then call the Azure Queue Storage client API method to push a message into the queue:

SendMessage(string queuedMessage);

Next, we check our Azure storage queue in the Azure portal.

The message should be there with a unique message id:

You have seen how to post a message to the Azure storage queue.

In future posts I will be making use of the storage queue for more interesting application integrations including Azure function orchestrations.

That is all for today’s post.

I hope you have found this post useful and informative.

Social media & sharing icons powered by UltimatelySocial