articles

Create Queue Service in Windows Azure

Chris Anderson12559 23-Jan-2012

Queue Service is a windows azure storage type where we can store data on basis of queue concept i.e. first in first out. It will display those message first which was processed earlier.

The Queue service stores message that may be read by any client who has access to the storage account. A queue can contain an unlimited number of messages, each of which can be up to 64 KB in size using version 2011 – 08 – 18 or newer. For previous versions, the maximum size of a message is 8 KB. Messages are generally added to the end of the queue and retrieved from the front of the queue.

If you need to store messages larger than 64 KB, you can store message data as a blob or in a table, and then store a reference to the data as a message in a queue.

The Queue service exposes the following resources via the REST API:

·         Account: The storage account is a uniquely identified entity within the storage system. The account is the parent namespace for the Queue service. All queues are associated with an account.

·         Queue: A queue stores messages that may be retrieved by a client application or service.

·         Messages: Messages are XML-compliant and may be up to 8 KB in size.

Now I am going to explain how to create queue and how to add a message in a queue in a windows azure application.

·         Open Microsoft Visual Studio 2010 as an administrator.

·         To create a new project FileNewProject.

·         Select Cloud template from the Installed Templates.

·         Select Windows Azure Project and enter the name of the project as  QueueService.

·         Click OK to proceed.

Create Queue Service in Windows Azure

·         To add an asp.net web role to the solution, choose ASP.NET Web Role and then choose the right arrow. The roles are displayed in the Windows Azure solution pane of the dialog box. You can rename it as QueueService.

·         Click OK to add.

Create Queue Service in Windows Azure

After adding web role, delete all the files from the PersonalInfo web role in the solution explorer except ReferencesPropertiesWebRole.cs.

·         Add a web form (Default.aspx) in a web role.

·         Add a new web.config file.

·         Modify the Default.aspx as shown below:

 <table>
        <tr>
            <td>
                <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="5" Columns="50"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="left">
                <asp:Button ID="btnAddMsg" runat="server" Text="Add Message" onclick="btnAddMsg_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblMsgs" runat="server"></asp:Label>
            </td>
        </tr>
    </table>

 Add the following additional namespace in a Default.aspx.cs :

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

After adding namespace, add the following code on the click event of button to create a queue service and add a message in a queue:

 protected void btnAddMsg_Click(object sender, EventArgs e)
        {
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, 
             configSettingPublisher) =>              configSettingPublisher) =>
            {
                var connectionString = RoleEnvironment.GetConfigurationSettingValue 
                (configName);                 (configName);
                configSettingPublisher(connectionString);
            });
 
            CloudStorageAccount storageAccount = 
                                 CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            CloudQueueClient queueClient =storageAccount.CreateCloudQueueClient();
            CloudQueue queue =queueClient.GetQueueReference("msgservice");
            queue.CreateIfNotExist();
            CloudQueueMessage message = new CloudQueueMessage(txtMessage.Text);
            queue.AddMessage(message);
           
            message= queue.GetMessage();
            lblMsgs.Text= message.AsString;
        }
 

  You also have to add the setting (DataConnectionString) in a web role configuration file:

·         Right click on web role, select Properties.

·         Move to Settings tab.

·         Add setting (DataConnectionString) as shown in below figure:

Create Queue Service in Windows Azure

Now press F5 to run your application. When you enter some text and click on Add message, it will add the message in a queue and display it in the label. The output should something like below:

Create Queue Service in Windows Azure

I think after reading this article you can easily create queue and store messages in a queue in a windows azure application.

 


Updated 06-Jul-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By