Two Roles and a Queue Lab from Boston Azure Firestarter
At the Firestarter event on May 8, 2010, I spoke about Roles and Queues and worked through a coding lab on same. The final code is available in a zip file. The Boston Azure Firestarter – Bill Wilder – Roles and Queues deck can be downloaded – though since there were so many questions we didn’t get to covering a number many of the slides! – this was a hot topic!
The remainder of this post contains the narrative for the LAB we did as a group at the Firestarter. It probably will not stand alone super well, but may be of interest to some folks, so I’ve posted it.
- The TEMPORARILY Running Visualizer: http://baugfirestarter.cloudapp.net/
- The TEMPORARILY Running Roles and Queues Sample: http://bostonazuresample.cloudapp.net/
- These are “temporary” since the tokens for the accounts they are running expire soon (mid/late May or early June); the live instances will automatically stop working at that time.
The following procedure assumes Microsoft Visual Web Developer 2010 Express on Windows 7. The same general steps apply to Visual Studio 2008, Visual Studio 2010, and Web Developer 2008 Express versions, though details will vary.
0. Open Microsoft Visual Web Developer 2010 Express and select File | New Project
1. Select Windows Azure Service and click Okay:
If you have trouble finding the Windows Azure Service template, you can type “Azure” into the search box in the top-right to narrow the options. Also, if you don’t have the Windows Azure SDK installed, you will need to install that before proceeding – but there will be a link provided by Visual Web Developer 2010 Express that will direct you to the right page. Install it if you need to and try again up to this point.
2. You will see a special dialog box for New Cloud Service Project from which you will add both a Web Role…
and a Worker Role…
Verify that both WebRole1 and WorkerRole1 are in the list on the right side, then click OK.
3. Before you begin making code changes, you can run your new application. You can run it in the debugger by pressing the F5 key.
You will probably get the following error message:
The error message is telling you that you need to close Visual Web Developer 2010 Express and restart it with elevated privileges.
4. To start any Windows program with elevated privileges , right-click on the application then choose Run as administrator from the pop-up menu:
Before it obeys your request to run as administrator, Windows 7 will double-check by popping up a security dialog.
Now you can reload your project and try running it again. The app should run and you should see a blank web browser page.
5. Once you’ve proven your application runs, it is time to make some changes.
Make the code changes indicated for the Two Roles and A Queue Lab in CODING STEP 1.
Note: the “coding step 1” and future coding steps were handouts (paper!) at the Boston Azure Firestarter on Sat May 8, 2010. In lieue of reproducing them here, I will post the final solution.
This lab will establish some WebRole basics.
6. When done applying CODING STEP 1, run the application again.
7. After demonstrating your application runs, Deploy it to Azure.
This is a simple application so it helps us get through the initial deployment with minimal challenges.
8. Apply CODING STEP 2 – Add Queue (in local dev fabric storage)
9. CODING STEP 3 – Add “DumpQueue” method and “FirestarterWebRoleHelpers.cs”
You will get the following dialog box – type “code file” into the search area on the top-right, select Visual C# Code File, and type in the filename “FirestarterWebRoleHelpers.cs” as shown and click Add:
The new file “FirestarterWebRoleHelpers.cs” will open in the editor. It should be empty to begin with. Cut and Paste in the contents from http://bostonazure.org/files/FirestarterWebRoleHelpers.cs.txt.
Why? The contents of this file has little to do with Windows Azure, so we don’t want to focus on it. But we want to use some utility routines from it so that we can focus on Azure concepts.
10. CODING STEP 4 – Adding Cloud-based Queue
First we need to configure the cloud.
Go to http://windows.azure.com and log in. You may wish to consult instructions on redeeming a token at https://blog.codingoutloud.com/2010/05/06/redeeming-an-azure-token/ or http://bit.ly/dgCuMn
Your storage account has a subdomain, as circled above. This – and the Access Key – need to be added to your Web Role and Worker Role so that they can access (and share the same queue within) cloud-hosted storage.
Right-click in Visual Studio on the WebRole1, select Properties, and select the Settings tab on the left. It will appear something like this:
Now click on Add Setting and give the new item the name “DataConnectionString”, the Type “Connection String”, and click on the “…”
This will bring up the Storage Connection String editor – fill in the fields – where your “Account name” is the same as the subdomain shown on the Storage Service (see above – in that screen shot it is “bostonazurequeue”) and the Key can be either Primary or Secondary Access Key (from same area in the Azure Portal):
You are NOT DONE in the screen yet. Also add a Setting named “StatusUpdateQueueName”– of Type “String” – with Value “updatemessagequeue1” as follows:
11. Now REPEAT BOTH STEPS for WorkerRole1.
Yes, add both Settings also to WorkerRole1 – they both will end up with the same settings. You can “cheat” with cut and paste in the .cscfg and .csdef files.
12. Enable Cloud-hosted Queue from Web Role
Now you are ready go on to make the code changes to use this new configuration item.
Apply CODING STEP 4: Enabling the Cloud-hosted Queue from the Web Role
Now run your application using cloud storage for the queue:
Note that you can also examine the contents of the queue online by visiting http://myAzureStorage.com and providing the same credentials you used when setting up the DataConnectionString above for both the Web and Worker roles.
13. Enable Cloud-hosted Queue from Worker Role
Now you are ALMOST ready go on to make the code changes to use this new configuration item.
Before applying the coding, we need to add a project reference (otherwise you won’t be able to Resolve use of networking classes used in the FirestarterWorkerRoleHelpers.). In Visual Studio on the right side, under the Solution Explorer, right-click on the References element underneath WorkerRole1 and select Add Reference, then from the .NET tab, select System.Web and click okay:
Also, similar to step 9 above, add a new Code File called “FirestarterWorkerRoleHelpers.cs” to hold some additional needed (but not core to Azure) code.
The new file “FirestarterWorkerRoleHelpers.cs” will open in the editor. It should be empty to begin with. Cut and Paste in the contents from http://bostonazure.org/files/FirestarterWorkerRoleHelper.cs.txt.
Now you can apply Apply CODING STEP 5: Enabling the Cloud-hosted Queue from the Worker Role.
14. Deploying to Staging Area in Cloud to Staging
15. Cutover from Staging to Production
16. Add in secret Twitter posting code from your Worker Role…
Yes, this can be done by including a hash character (#) as part of the message you type into your web application.