Contents
Introduction
In our previous article, we have seen
five simple steps to create a web role application. Web role projects in Azure are like web applications. Azure has one more type of project,
Worker role. Worker role applications are background processing applications like Windows processes which run in the background. In this article, we will try to understand
the six basic steps to create a worker role project and as we run through the article, we will try to understand the various fundamental methods which are executed in worker role projects.
If you are really lazy like me, you can download my two Azure basic videos which explain what Azure is all about:
My Other Azure FAQ Articles
Please feel free to download my free 600+ questions and answers eBook which covers .NET, ASP.NET, SQL Server, WCF, WPF, WWF@ http://www.questpond.com.
Step 1: Ensure You Have Things In Place
In case you are a complete fresher to Azure, please ensure you have all the pre-requisites in place. You can read the following article to get the basic prerequisites:
Step 2: What Will We Do?
Worker roles are nothing but background processes which run on the Windows Azure platform. We will create a simple background process which will run X number of times and every time it runs, it will wait for 10000 ms.
Step 3: Select the Worker Role Template
Create a new project using the Worker role template as shown below:

Step 4: Import Namespaces
We need to import two namespaces, Microsoft.WindowsAzure.Diagnostics
and Microsoft.WindowsAzure.ServiceRuntime. Diagnostic will help us to display
a message using trace on the Azure profiler while ServiceRuntime provides functions for Azure services.

Step 5: Create Class and Override
Run and OnStart Methods
The next step is to add a class and override the OnStart and Run methods. In the below code snippet, we have created a simple WorkerRole class which inherits from RoleEntryPoint.
We have also defined a simple loop count variable called intLoops which is initialised to value 5. This value is initialised in the OnStart method.
The OnStart method is executed the first time your worker role is executed.

Now override the Run method with a simple loop which decrements the loop count and has a thread which sleeps for 10000 ms as every loop is executed.

Step 6: Run the Project and Watch the Azure Console
Now run the worker role and see your Azure console. You should see that one worker role instance is running.

We have displayed trace information at various places in the start and run methods. You can see in the Azure prompt the number of loops executed in
the Azure diagnostic.
Event=Information,Level=Info,ThreadId=4148,=This is loop number 5
Event=Information,Level=Info,ThreadId=4148,=This is loop number 4
Event=Information,Level=Info,ThreadId=4148,=This is loop number 3
Event=Information,Level=Info,ThreadId=4148,=This is loop number 2
Event=Information,Level=Info,ThreadId=4148,=This is loop number 1
History
- 4 January, 2010: Initial post.