Simple 6 steps to run your first Azure Worker Role Program


Introduction

In our previous article, we have seen 5 simple steps to create web role application. Web role projects in Azure are like web applications. Azure has one more type of project i.e. worker role. Worker role applications are back ground processing application like windows process which runs on the back ground. In this article we will try to understand 6 basic steps to create 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 Azure Faq Part 1 :- Video1 Azure Faq Part 2 :- Video2.


Please feel free to download my free 500 question and answer eBook which covers .NET , ASP.NET , SQL Server , WCF , WPF , WWF@ http://www.questpond.com

Step 1:- Ensure you have things at place

In case you are a complete fresher to Azure, please ensure you have all the pre-requisite at place. You can read the below article to get the basic prerequisite http://www.c-sharpcorner.com/UploadFile/shivprasadk/136/Default.aspx?ArticleID=06ceb214-4077-4380-81b0-7d2c56021193  

Step 2:- What will we do?

Worker roles are nothing but back ground process which runs on windows azure platform. We will create a simple background process which will run for X number of times and every time it runs, it will wait for 10000 MS. 

Step 3:- Select the worker role template

So create a new project using the worker role template as shown below.
 

1.jpg 

Step 4:- Import namespaces

We need to import two namespaces one is 'Microsoft.WindowsAzure.Diagnostics' and 'Microsoft.WindowsAzure.ServiceRuntime'. Diagnostic will help us to display message using trace on the azure profiler while ServiceRuntime provides functions for azure services. 

2.jpg

Step 5:- Create class and override run and onstart method

The next step is to add a class and override the 'OnStart' method and 'Run' method. In the below code snippet we have created a simple 'WorkerRole' class which inherits from 'RoleEntryPoint'.

We have also defined as simple loop count variable called as 'intLoops' which is initialized to value 5. This is value is initialized in the 'OnStart' method. 'OnStart' method is executed the first time your worker role is executed. 


3.jpg
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.

4.jpg

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. 

5.jpg
We had displayed trace information at various places in start and run method. You can see in the Azure prompt the number of loops executed in 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


Similar Articles