Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
Lab 10: .NET multi-tier application using Azure Service Bus queues
In this tutorial you’ll build and run the multi-tier application in an Azure cloud service. The front end is an ASP.NET MVC web role and the back end is a worker-role that uses a Service Bus queue. You can create the same multi-tier application with the front end as a web project that is deployed to an Azure website instead of a cloud service.
Scenario overview: inter-role communication
To submit an order for processing, the front-end UI component, running in the web role, must interact with the middle tier logic running in the worker role. This example uses Service Bus messaging for the communication between the tiers.
Using Service Bus messaging between the web and middle tiers decouples the two components. In contrast to direct messaging (that is, TCP or HTTP), the web tier does not connect to the middle tier directly; instead it pushes units of work, as messages, into Service Bus, which reliably retains them until the middle tier is ready to consume and process them.
Service Bus provides two entities to support brokered messaging: queues and topics. With queues, each message sent to the queue is consumed by a single receiver. Topics support the publish/subscribe pattern in which each published message is made available to a subscription registered with the topic. Each subscription logically maintains its own queue of messages. Subscriptions can also be configured with filter rules that restrict the set of messages passed to the subscription queue to those that match the filter.
The following example uses Service Bus queues.
As load increases, more worker processes can be added to read from the queue. Each message is processed by only one of the worker processes. Furthermore, this pull-based load balancing enables optimal use of the worker machines even if the worker machines differ in terms of processing power, as they will pull messages at their own maximum rate. This pattern is often termed the competing consumer pattern.
Level 3 Asia Pacific University of Technology & Innovation Page 1 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
a. Create a namespace
Estimation time for this section A: 5 Minutes
The first step is to create a namespace, and obtain a Shared Access Signature (SAS) key for that namespace. A namespace provides an application boundary for each application exposed through Service Bus. A SAS key is generated by the system when a namespace is created. The combination of namespace name and SAS key provides the credentials for Service Bus to authenticate access to an application.
To begin using Service Bus messaging entities in Azure, you must first create a namespace with a name that is unique across Azure. A namespace provides a scoping container for addressing Service Bus resources within your application.
1. In a new browser window, sign in to the Azure Portal.
2. Click Create a resource > Integration > Service Bus.
3. In the Create namespace dialog, enter a namespace name.
4. After making sure the namespace name is available, choose the pricing tier (Basic, Standard, or Premium). If you want to use topics and subscriptions, make sure to choose either Standard or Premium. Topics/subscriptions are not supported in the Basic pricing tier.
Level 3
Asia Pacific University of Technology & Innovation Page 2 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
5. 6. 7. 8.
In the Subscription field, choose an Azure subscription in which to create the namespace.
In the Resource group field, choose an existing resource group in which the namespace will live, or create a new one.
In Location, choose the country or region in which your namespace should be hosted.
Click Create. The system now creates your namespace and enables it. You might have to wait several minutes as the system provisions resources for your account.
Level 3
Asia Pacific University of Technology & Innovation Page 3 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
b. Obtain the management credentials
Estimation time for this section B: 5 Minutes
Creating a new namespace automatically generates an initial Shared Access Signature (SAS) rule with an associated pair of primary and secondary keys that each grant full control over all aspects of the namespace.
To copy the initial rule, follow the below steps.
1.
2.
Click All resources, then click the newly created namespace name.
In the namespace window, click Shared access policies. In the Shared access policies screen, click RootManageSharedAccessKey.
In the Policy: RootManageSharedAccessKey window, click the copy button next to Primary Connection String, to copy the connection string to your clipboard for later use. Paste this value into Notepad or some other temporary location.
Repeat the previous step, copying and pasting the value of Primary key to a temporary location for later use.
3.
4.
Level 3
Asia Pacific University of Technology & Innovation Page 4 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
c. Create a web role
Estimation time for this section C: 5 Minutes
In this section, you build the front end of your application. First, you create the pages that your application displays. After that, add code that submits items to a Service Bus queue and displays status information about the queue.
1. 2.
Open Visual Studio. From the main menu, select File > New > Project.
3.
From the Roles pane, double-click ASP.NET Web Role.
In the New Project dialog box, select Web > Cloud > Azure Cloud
Service > MultiTierApp. Then select OK.
Level 3
Asia Pacific University of Technology & Innovation Page 5 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
4.
5.
6. 7.
Hover over WebRole1 under Azure Cloud Service solution, click the pencil icon,
and rename the web role to FrontendWebRole. Then click OK. (Make sure you
enter “Frontend” with a lower-case ‘e,’ not “FrontEnd”.).
From the New ASP.NET Project dialog box, in the Select a template list,
click MVC.
In Solution Explorer, in the FrontendWebRole project, right-click References, then
click Manage NuGet Packages.
Click the Browse tab, then search for WindowsAzure.ServiceBus. Select
the WindowsAzure.ServiceBus package, click Install, and accept the terms of use.
Level 3 Asia Pacific University of Technology & Innovation Page 6 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
8. In Solution Explorer, right-click Models and click Add, then click Class. In the Name box, type the name OnlineOrder.cs. Then click Add.
Level 3 Asia Pacific University of Technology & Innovation Page 7 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
d. Write code for your web role.
Estimation time for this section C: 15 Minutes
In this section, you create the various pages that your application displays.
1.
In the OnlineOrder.cs file in Visual Studio, replace the existing namespace definition with the following code.
In Solution Explorer, double-click Controllers\HomeController.cs. Add the following using statements at the top of the file to include the namespaces for the model you just created, as well as Service Bus.
Also in the HomeController.cs file in Visual Studio, replace the existing namespace definition with the following code. This code contains methods for handling the submission of items to the queue.
2.
3.
Level 3
Asia Pacific University of Technology & Innovation Page 8 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
4. 5.
From the Build menu, click Build Solution to test the accuracy of your work so far. Add an Submit view
In Solution Explorer, expand the Views folder and right click the Home folder, click Add. Then, click View.
In the Add View dialog box, do the following:
Now, change the displayed name of your application. In Solution Explorer, double- click theViews\Shared\_Layout.cshtml file to open it in the Visual Studio editor.
Replace all occurrences of My ASP.NET Application with Northwind Traders Products and remove the Home, About, and Contact links.
In the View name box, type Submit.
In the Template box, select Create.
In the Model class box, select OnlineOrder (FrontendWebRole.Models).
Click Add.
6. 7.
Level 3
Asia Pacific University of Technology & Innovation Page 9 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
8.
Finally, modify the submission page to include some information about the queue. In Solution Explorer, double-click the Views\Home\Submit.cshtml file to open it in the Visual Studio editor. Add the following line after
Submit
. For now, the ViewBag.MessageCount is empty. You will populate it later
You now have implemented your UI. You can press F5 to run your application and confirm that it looks as expected.
9.
Level 3
Asia Pacific University of Technology & Innovation Page 10 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
e.
Write the code for submitting items to a Service Bus Queue
Estimation time for this section E: 20 Minutes
Now, add code for submitting items to a queue. First, you create a class that contains your Service Bus queue connection information. Then, initialize your connection from Global.aspx.cs. Finally, update the submission code you created earlier in HomeController.cs to actually submit items to a Service Bus queue.
1.
In Solution Explorer, right-click FrontendWebRole (right-click the project, not the role). Click Add, and then click Class. Name the class QueueConnector.cs. Click Add to create the class
Now, add code that encapsulates the connection information and initializes the connection to a Service Bus queue. Replace the entire contents of QueueConnector.cs with the following code, and enter values for your Service Bus namespace (your namespace name) and
2.
Level 3
Asia Pacific University of Technology & Innovation Page 11 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
3. Add the below method within the public class Queue Connector{}.
4. Now, ensure that your Initialize method gets called. In Solution Explorer, double- click Global.asax\Global.asax.cs. Add the following line of code at the end of the Application_Start method.
5. In Solution Explorer, double-click Controllers\HomeController.cs.
Level 3
Asia Pacific University of Technology & Innovation Page 12 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
6. Update the Submit() method (the overload that takes no parameters) as follows to get the message count for the queue
7. Update the Submit(OnlineOrder order) method (the overload that takes one parameter) as follows to submit order information to the queue.
8. You can now run the application again. Each time you submit an order, the message count increases.
Level 3
Asia Pacific University of Technology & Innovation Page 13 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
Screen shot before sending to the service bus queue.
Screen shot after sent the orders into the service bus queue.
Order 1
Level 3 Asia Pacific University of Technology & Innovation Page 14 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC)
.Net Multi-Tier Application using Azure Service Bus Queue
Order 2
Result in the Current Service Bus Queue
Level 3 Asia Pacific University of Technology & Innovation Page 15 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
f.
Create the worker role.
Estimation time for this section F: 20 Minutes
You will now create the worker role that processes the order submissions. This example uses the Worker Role with Service Bus Queue Visual Studio project template. You already obtained the required credentials from the portal.
1. Make sure you have connected Visual Studio to your Azure account.
2. In Visual Studio, in Solution Explorer right-click the Roles folder under the
MultiTierApp project.
3. Click Add, and then click New Worker Role Project. The Add New Role
Project dialog box appears.
4. In the Add New Role Project dialog box, click Worker Role with Service Bus Queue.
Level 3
Asia Pacific University of Technology & Innovation Page 16 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
5. In the Name box, name the project OrderProcessingRole. Then click Add.
6. Copy the connection string from the shared access policies section. Then,
in Solution Explorer, right-click the OrderProcessingRole you created in step 5 (make sure that you right-click OrderProcessingRole under Roles, and not the class). Then click Properties.
7. On the Settings tab of the Properties dialog box, click inside the Value box
for Microsoft.ServiceBus.ConnectionString, and then paste the endpoint value you copied in step 6.
8. 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 the OrderProcessingRole class (right-click the class icon, not the role). Click Add, then click Existing Item.
Level 3
Asia Pacific University of Technology & Innovation Page 17 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
9. Browse to the subfolder for FrontendWebRole\Models, and then double- click OnlineOrder.cs to add it to this project.
10. In WorkerRole.cs, change the value of the QueueName variable
from “ProcessingQueue” to “OrdersQueue” as shown in the following code.
11. Add the following using statement at the top of the WorkerRole.cs file
12. In the Run() function, inside the OnMessage() call, replace the contents of the try clause with the following code
Level 3
Asia Pacific University of Technology & Innovation Page 18 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
13. Complete modify screenshot as below:
14. You have completed the application. You can test the full application by right- clicking the MultiTierApp project in Solution Explorer, selecting Set as Startup Project, and then pressing F5.
Note that the message count does not increment, because the worker role processes items from the queue and marks them as complete. You can see the trace output of your worker role by viewing the Azure Compute Emulator UI.
You can do this by right-clicking the emulator icon in the notification area of your taskbar and selecting Show Compute Emulator UI.
Level 3
Asia Pacific University of Technology & Innovation Page 19 of 20
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) .Net Multi-Tier Application using Azure Service Bus Queue
Summary:
In this tutorial, we learned how to create a .NET multi-tier application using Azure Service Bus queues.
Level 3 Asia Pacific University of Technology & Innovation Page 20 of 20