Windows Azure Compute Node

Posted: September 11, 2011 in Uncategorized

In one of my following blogs I explained the difference between the 2 main roles Web Role and Worker Role. In this example I will show you how to use the 2 compute roles and to link between them using the queue storage.

First of all open your visual studio and run it as administrator because the azure tools require elevated permissions to work on your local machine.

Now on in your Visual Studio create a new project and choose the cloud under C#

Now press “OK” you will find another window open you will add the roles need available to be used for the development on Windows Azure.

In this Blog we will use main 2 roles:

  • ASP .NET Web Role
  • Worker Role

Now delete the 2 forms added in the webRole1 project “Default” and “About”, then add another form and let’s call it webform 1

We will add a textbox in the webapplication and a button. In the button’s code, I will show you how to use the queue storage to send a string for example to trigger a certain event to run on the server side consuming the processing power if the VMs.


<asp:TextBox
ID=”TextBox1″ runat=”server”></asp:TextBox>

 


<asp:Button
ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button”
/>

 

 

Now in the webRole1 we should add the required library to be able to develop on Windows Azure. So we will add the followings:

  • Microsoft.WindowsAzure.StorageClient
  • Microsoft.WindowsAzure.ServiceRuntime

 

Now add the following libraries in your cloud:

 

    using Microsoft.WindowsAzure;

using Microsoft.WindowsAzure.StorageClient;

using Microsoft.WindowsAzure.ServiceRuntime;

 

Create the connection String on both the web role and worker role. In the cloud project “WindowsAzureProject1” double click on the roles, under the setting tab click on add setting and create your connection string to be able to access your storage. In this example I will name it “ConnectionString”

 


 

 

Now in the method “Button1_Click” we will add the following code to send the string added on the text box to the worker role

protected
void Button1_Click(object sender, EventArgs e)

{


var account = CloudStorageAccount.FromConfigurationSetting(“ConnectionString”);


var queue = account.CreateCloudQueueClient().GetQueueReference(“QueueName”);

 


var msg = new
CloudQueueMessage(TextBox1.Text);

queue.AddMessage(msg);

}

 

Now we have finished the webrole side lets move on to the woker role side. Open the worker role and do the same with connection string part

 

To receive the queue message, we will add this code to the onStart() method:

 

 

string txt = “<text>”;


var account = CloudStorageAccount.FromConfigurationSetting(“ConnectionString”);


var queue = account.CreateCloudQueueClient().GetQueueReference(“QueueName”);

 


var msg = queue.GetMessage();


if (txt == msg.ToString())

{


//trigger event or call a method

}


else

{


//you can stop the worker role process

}

 


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s