Be sure to complete the following exercise before Exercise 2.
Windows Azure Services – Exercise 1 - The Very Beginning – The Infamous Hello World
Learn about the API and Fabric
In this exercise, you will explore some of the Windows Azure API and development fabric features.
First, you will add some logging support. Logging is extremely important in Windows Azure applications.
Instead of attaching a debugger
This is your view on what is happening inside your application. While you can attach a debugger to the local development fabric, you cannot attach a debugger to the cloud.
Why logging is important
The Windows Azure log allows you to write out events required for you to troubleshoot your application. You will also see that any unhandled exceptions are recorded in the Windows Azure log too.
In the following task, you will provide additional instance configuration definitions in the ServiceDefinition.csdef file. This configuration can be read at runtime using the Windows Azure API. Instance configuration can be read by Web sites and worker processes. Instance configuration can be changed at runtime.
Matching Child Elements in the configuration Files
Local Storage - Where you can store your Azure Messages
Then, you will define a local storage to use a file to store messages. The local storage is not shared between instances.
The ServiceDefinition.csdef file also contains instance requirements, in the following task you will change the number of instances running in the local fabric. Instances is a way to control scalability. The more customers you have the more you will want to increase Instances.
Task: Adding Logging Support
Most developers want to be able to write to some type of log console. In order to implement logging support we will need to add one textbox and two buttons. This type of support makes it easier for developers to debug their applications.
Note: If you closed Visual Studio, be sure to open it elevated as Administrator, from Start | All Programs | Microsoft Visual Studio 2008 right-click Microsoft Visual Studio 2008 and choose Run as Administrator.
Steps to take
In the Default.aspx page, add a new ASP:Button control from the Toolbox to the div tag following the text you added previously.
Change the ID of the button to btnLog and set its Text property to Log This.
Add a new ASP:Button with an ID of btnThrowException, and set its Text property to Throw Exception.
Add a ASP:TextBox to the page just before the button btnLog. Change the ID for the ASP:TextBox to txtMessage. You may wish to format the page to look good, but this is not required.
The updated page should be similar to the following, including the "runat" attributes:
Note that in the image below we have added one text box and two buttons. The text box is where we will type in our message and the first button is used to write to the log console and the second button is used to throw an exception. This functionality is strictly to be used by a developer, not the end user.
Here is what the rendered page looks like.
Open Default.aspx in design mode and double-click the Log This button. Add the following procedure to process the btnLog_Click event.
Type in the following code:
It should look like this:
void btnLog_Click(object sender, EventArgs e)
Notice the event procedure in the file Default.aspx.cs
The code above indicates that the contents of the text box will be written to the log of the Role Manager. Noticed that there is only a single instance where the logged messages will appear.
Double click the "Throw Exception" and code it up as you see the page below. This is the whole "code behind" for default.aspx. Notice that we simply have to event procedures, one for each of the two buttons
Here is what the RoleManager log file looks like this, due to a null reference exception. The ToString() method is not valid, because the object "o" has not been instantiated.
Task– Debugging in the Development Fabric
Open the Default.aspx.cs file and set a break point at the beginning of the btnThrowException_Click method.
Press F5 to start debugging.
How to stop the running program
Stop the tenant. To do this, in the development fabric, select the tenant node (deployment(xx)) and click the Suspend button. This is similar to pausing your application.
Task – Using Instance Configuration
Open the ServiceDefinition.csdef file from the RDCompute project.
Define a new setting with the name "GreetingString" in the WebRole as follows:
Open the ServiceConfiguration.cscfg file from the RDCompute project.
In the WebRole role, add a GreetingString setting and set the value to "hello, world. Azure version", as follows below. This behaves like any standard configuration file and .net programming, allowing us to retrieve a value based on a key or name.
Open the Default.aspx.cs file.
In the Page_Load method, add the following code:
The code above that allows us to retrieve the value from our configuration file, as stated previously.
Caching space for your service - LocalStorage
The LocalStorage element identifies a local storage resource that provides caching space for the service. RDComputeStorage is a name that we can access from our code to allow us to easily manage local text files. Local text files are useful in many different types of programming environments and the cloud is no exception. You'll see RDComputeStorage he's being used in our code to create a path from which we can read and write the text file called message.txt.
We will add that to our ServiceConfiguration.csdef.
The fabric reserves storage space for the local store in the file system of a computer running within the fabric.
Space is allocated based on the values specified for the sizeInMb attribute.
The minimum amount of storage space that may be allocated is 1 MB.
Note that different roles running as part of the same service may have different names for the local store.
Required. A unique name for the local store.
Optional. The desired amount of storage space to allocate for the local store, in MB.
Adding some files to the Web Site – you will need to download these files
Download them here. Simply download these files after downloading them and add them to your project.
Your solution should look like this:
Explanation Of Files
MessageHelper.cs (class module)
This helper class is used to to retrieve and save messages. It only has two methods and these methods are used to write messages to the end of a text file in to read messages and put them inside of a list box.
It has two main methods: (1) GetMessages() (2) SaveMessage(). These methods will retrieve and save messages. This is simply a way to have text file in the cloud for us to store things.
The file name is messages.txt, which is a file stored in the cloud.
The way to get the path is:
ILocalResource resource = RoleManager.GetLocalResource("RDComputeStorage");
String the_path = resource.RootPath;
RDComputeStorage is the name we gave to local storage in ServiceDefinition.csdef
SaveMessage() Method – used to add text strings to the bottom of the text file. This text file lives in the cloud.
Note we can write to the RoleManager's message log and to the messages.txt file.
Understanding Shoutbox.aspx – a web page that is used to view and add strings from and to a text file. That text file is called messages.txt.
Controls being used
Facts – a several controls have been added to the web page called Shoebox.aspx
To hold strings from messages.txt
Holds a text string that will be saved in messages.txt, after a user clicks the "SendButton"
Saves the string that the user typed into MessageTextBox to the bottom of messages.txt
Used to update MessagesListBox every 3 seconds. This application uses a polling mechanism to populate the ListBox.
An AsyncPostBackTrigger Class is used. It is used because it enables controls to be triggers for an UpdatePanel control. Controls that are triggers for an update panel cause a refresh of the panel's content after an asynchronous postback
Allows artial-page rendering, which removes the need for the whole page to be refreshed as the result of a asynchronous postback
We are ready to run the solution
First, set ShoutBox as the startup web page.
Hit F5 to run the solution. Type in a message and hit "Send"
Note that the ListBox got populated appropriately. A timer fires of every 3 seconds and populates the list box.
Also note that the RoleManager's log interface got updated as well.
In Visual Studio, stop debugging by pressing SHIFT+F5.
Task– Executing Multiple Instances in the Development Fabric
In this task, you will modify the configuration file to increase the number of instances that you wish to execute in the fabric. This allows you to test your application in a multi-instance environment, similar to that found in the cloud. And this approach is especially useful in terms of increasing scalability. Increasing that count to "2" allows IIS two spin up two the more dot net programs to work alongside IIS.
Set the target instances to two. To do this, open the ServiceConfiguration.cscfg file, locate the Instances element and set the value of the count attribute to 2.
Press F5. The development fabric should load with two instances running.
Open a new instance (not a new tab) of the browser and navigate to http://127.0.0.1:81/shoutbox.aspx (specifying a port number if appropriate).
Enter several messages from each browser window. You should see requests hit one or another instance.
Note: Potentially you may see one instance handle all the requests from your single client. This is likely due to IE recycling the same TCP connection. To improve your chances of reaching the second tenant, launch multiple instance of Internet Explorer instead of opening a new Tab or New Window
In order to get this sample to send messages to two different tenants, I had to start multiple instances of Internet explorer. It took three instances of Internet explorer to be able to write messages to tenant number 1, as seen by the red boxes.
Refers to first browser instance. Writes to Tenant 0.
Refers to the second browser instance. Writes to Tenant 1.
This concludes Exercise 2: Configuration, Logging, and Debugging. Exercise two provides critical knowledge to understanding how to program Azure and take advantage of the built in features provided by a Visual Studio.
PingBack from http://mdavey.wordpress.com/2009/01/20/azure-invitation-code/
It was a pleasure to get back out on the road with Chris and Jim and see all the folks who took