Export (0) Print
Expand All

Creating an Azure Worker Role with Service Bus Queue Project

Updated: March 10, 2015

Microsoft Azure Tools for Microsoft Visual Studio supports the Azure Worker Role with Service Bus Queue project template type. You can use this template to add a worker role to your Microsoft Azure application that receives messages from a Service Bus queue. For more information about project templates in the Microsoft Azure Tools for Microsoft Visual Studio, see Creating an Azure Project with Visual Studio.

This section describes how to create a Service Bus queue worker role. The code is based on the .NET Multi-Tier Application Using Service Bus Queues tutorial, but replaces the worker role creation in that tutorial with one that uses the Service Bus project template in the Microsoft Azure Tools.

Microsoft Azure contains APIs in the SDK that provide a consistent way for developers to create new instances of clients across services, such as Service Bus. The new Service Bus Queue Worker Role project template uses these APIs in the generated code. For more information about the configuration changes, see Configuration Changes.

To install the Microsoft Azure Tools for Microsoft Visual Studio, see the section Installing the Azure Tools for Microsoft Visual Studio. If you have already installed the Microsoft Azure Tools, proceed to the next step.

Perform the steps in .NET Multi-Tier Application Using Service Bus Queues, up to (but not including) the “Create the Worker Role” step. After you have completed the “Create a Web Role” section in the MultiTier tutorial, do not proceed to the “Create the Worker Role” section. Instead, proceed to the following step.

  1. In Visual Studio, in Solution Explorer right-click the Roles folder under the MultiTierApp project.

  2. Click Add, and then click New Worker Role Project. The Add New Role Project dialog appears.

  3. In the Add New Role Project dialog, click Worker Role with Service Bus Queue, as in the following figure:

    Choosing a Service Bus Worker Role
  4. In the Name box, name the project OrderProcessingRole. Then click Add.

  5. In Server Explorer, add a connection to the service namespace you want to use, as described in Browsing Service Bus Resources with the Visual Studio Server Explorer.

  6. In Server Explorer, right-click the name of your service namespace, then click Properties. In the Visual Studio Properties pane, the first entry contains a connection string that is populated with the service namespace endpoint containing the required authorization credentials. For example, see the following figure. Double-click ConnectionString, and then press Ctrl+C to copy this endpoint to the clipboard.

    Service Bus Namespace proprties
  7. In Solution Explorer, right-click the OrderProcessingRole you created in the previous step, then click Properties.

  8. In the Settings tab of the Properties dialog, click inside the Value box for Microsoft.ServiceBus.ConnectionString, and then paste the endpoint value you copied in step 6.

    Service Bus Worker Role Properties
  9. Create an OnlineOrder class to represent the orders as you process them from the queue. You can reuse a class you have already created. In Solution Explorer, right-click OrderProcessingRole. Click Add, then click Existing Item.

  10. Browse to the subfolder for FrontendWebRole\Models, and double-click OnlineOrder.cs to add it to this project.

  11. Replace the value of the QueueName variable in WorkerRole.cs from “ProcessingQueue” to “OrdersQueue” as in the following code:

    // The name of your queue
    const string QueueName = "OrdersQueue";
  12. Add the following using statement at the top of the WorkerRole.cs file:

    using FrontendWebRole.Models;
  13. In the Run() function, add the following code inside the if (receivedMessage != null) loop, below the Trace statement:

    if (receivedMessage != null)
            // Process the message
            Trace.WriteLine("Processing", receivedMessage.SequenceNumber.ToString());
            // Add this code
            // View the message as an OnlineOrder
            OnlineOrder order = receivedMessage.GetBody<OnlineOrder>();
            Trace.WriteLine(order.Customer + ": " + order.Product, "ProcessingMessage");

You can now run the application by pressing F5 within Visual Studio. Note that the message count does not increment, because the worker role processes items from the queue and marks them as complete.

You can view the trace output of the worker role by opening the Azure Compute Emulator. To do this, right-click the emulator icon in the notification area of your taskbar and click Show Compute Emulator UI. As you enter data on the website, it is processed in the emulator, as in the following figure:

Windows Azure Compute Emulator UI

© 2015 Microsoft