Simple 6 steps to run your first Azure Worker Role Program

Questpond
Posted by in Azure category on for Beginner level | Views : 15257 red flag

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.

Simple 6 steps to run your first Azure Worker Role Program

 

Introduction

Step 1:- Ensure you have things at place

Step 2:- What will we do?

Step 3:- Select the worker role template

Step 4:- Import namespaces

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


Step 6:- Run the project and watch the Azure console


 

Introduction


In our previous article http://www.dotnetfunda.com/articles/article482-windows-azure-faq-part-1-.aspx.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.dotnetfunda.com/articles/article758-simple-5-steps-to-run-your-first-azure-program-.aspx   .
 

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.
 

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.
 

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.
 

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 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
Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Jaikishen.Jayadev on: 4/9/2014 | Points: 25
Hi Shiv.. I'm using VS2012 and I followed your steps.. Most of the steps were already part of the solution,. Having an issue with the following line:
RoleEnvironment.Changing += RoleEnvironment.Changing;

The error says
"Error	1	The event 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.Changing' can only appear on the left hand side of += or -=	c:\users\jay\documents\visual studio 2012\Projects\WindowsAzureWorkerRoleSample\MyWorkerRole\WorkerRole.cs	36	57	MyWorkerRole"

Login to post response

Comment using Facebook(Author doesn't get notification)