Creating a Worker Role
In this article we are creating a worker role using Visual Studio 2010. This
will make the reader familiar with Windows Azure. After that the role is
deployed into the online portal.
Open Visual Studio 2010 and use the command New Project. From the appearing
dialog box select the Windows Azure Project option from the Visual C# group.
Enter an appropriate name for the project and click Ok to continue. Now you will
be prompted with another dialog for selecting the type of project.
Double click on the Worker role option to select a worker role project and click the Ok button.
Now you are ready with the Worker role project.
Purpose of Worker Role
The Worker role can be used to host WCF services, provide endpoints, perform
operations etc.
The default Run() method implementation contains an infinite loop as shown
below:
public
override
void
Run()
{
//
This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("WorkerRole1
entry point called",
"Information");
while
(true)
{
Thread.Sleep(10000);
Trace.WriteLine("Working",
"Information");
}
}
Now execute the application and you will be able to see the application inside
the browser. From the system tray, you can verify the Windows Azure Emulator is
running.
If successfully executed, you can see the trace output messages as shown below.
Exceptional Cases
Deploying the Worker Role
After developing the Worker Role, we have to package it and deploy to the cloud.
The entire steps in this are:
Create Worker Role
Create Package
Deploy to Cloud
Creating the Worker Role is already done. Now we need to do the following steps.
Create Package
For creating the package, right click on the project context menu and click
Package.
Now as dialog box will be appearing, leave the default options selected and
click the Package button on the dialog.
After performing the packaging, the folder containing package is opened in a new
explorer window automatically.
There are two files in the folder as shown above:
Configuration File
Service Package File
The Configuration file contains the instance count, application setting entries
etc.
Deploy to Cloud
The deploying part contains deploying the package file and the configuration
file. For deploying the files, we need to sign in to the Windows Azure Portal.
After signing in, click on the New Hosted Service button from the left top
panel. The following dialog box appears.
Detailing Input Fields in Dialog Box
The subscription list automatically selects the default subscription (trial).
In the service name text box, enter a name for the service.
In the service URL prefix text box, enter a unique prefix name.
In the region, select an appropriate region of deployment.
Enter an appropriate deployment name in the final text box.
Select Package
Now, we need to specify the Package location and Configuration file. Use the
Browse locally… button for these and select the files from your local machine.
After specifying all the inputs, click the Ok button. (In case of instance
warning box, click Yes to override it for time being)
Now you can see that the entry is shown as preparing to upload in the list.
Waiting for Upload Operation
We need to wait a few minutes for the upload operation to be completed. After
the upload operation is completed the instance will be started.
After all the operations are completed the status will turn to ready as show in
the image below:
Getting the Deployed Url
The DNS name specifies the url to access the worker role as shown below:
For viewing the deployed Worker Role traces we need to change the trace
configuration. This will be discussed later.
Warning
As the worker role we uploaded is having infinite loop, it will be quoting
computing resources and any usage beyond monthly free limit will be charged to
the credit card. So it is recommend you to delete the deployed worker role after
the test.
Deletion is explained below.
Deleting the Worker Role
As mentioned already, any computing hours excess of the trial limit will be
charged. So we need to ensure that the instances are stopped and deleted. The
steps are depicted below:
Step 1: Stopping an Instance
Sign in to the Windows Azure Portal, click the Hosted Services link to vie the
hosted services and click Stop from the toolbar.
The order of clicking is depicted as sequence.
Step 2: Deleting an Instance
Once the instance is stopped, click on the Delete button to delete the instance.
(This is for demonstration and one may keep the instance if it is needed in
future)
On clicking the Delete button you will be prompted with a confirmation dialog.
Click on the Yes button for proceeding with the Delete operation. Now you can
see the status being changed to Deleting…
After the operation is completed, the above entries are removed from the list.
So it confirms the Delete operation.
Summary
In this article we have seen how to create, package, deploy and delete a Worker
Role to windows azure hosting.