Click here to Skip to main content
15,868,141 members
Articles / Hosted Services / Azure

Six simple steps to run your first Azure Worker Role program

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
26 Feb 2014CPOL3 min read 122.1K   41   5
Six simple steps to run your first Azure Worker Role program.

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 watch my learn Azure Cloud and DevOps videos which explained thoroughly in step by step manner:

My Other Azure FAQ Articles

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:

Image 1

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.

Image 2

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.

Image 3

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.

Image 4

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.

Image 5

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 

Reference

Further reading

History

  • 4 January, 2010: Initial post.

For further reading do watch the below interview preparation and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionMy vote of 5 Pin
Assil25-Jun-18 15:34
professionalAssil25-Jun-18 15:34 
QuestionHow to create WorkerRole in windows azure? Pin
Member 917112827-Aug-12 0:46
Member 917112827-Aug-12 0:46 
QuestionHelp Worker Role Pin
fdevia1-Aug-12 13:39
fdevia1-Aug-12 13:39 
GeneralMy vote of 5 Pin
Sandeepkumar Ramani9-Sep-11 2:45
Sandeepkumar Ramani9-Sep-11 2:45 
GeneralMy vote of 5 Pin
Shahdat Hosain13-Jul-10 22:55
Shahdat Hosain13-Jul-10 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.