Simple 6 steps to run your first Azure Worker Role Program
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 on start method
Step 6:- Run the project and watch the Azure console
In our previous article http://www.dotnetspark.com/kb/702-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 .
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.dotnetspark.com/kb/1358-simple-5-steps-to-run-your-first-azure-program.aspx .
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.
So create a new project using the worker role template as shown below.
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.
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.
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