1

Hi I have created a new azure project with a worker role and have noticed a class WorkerRole.cs. I tried researching on what it's purpose is however I couldn't find a straight answer.

Adrian Bonnici
  • 283
  • 4
  • 14

2 Answers2

3

The goal of the WorkerRole.cs is to give you an entry point to execute "something" when the Worker Role instance starts. This could be a command line application you're starting, or a WCF service, or MySQL, ... anything you want.

Note that you'll need to write code to keep the instance 'alive'. If you don't do this, it will restart when the Run method completes. Take a look at the following question for more information: Using Thread.Sleep or Timer in Azure worker role in .NET?

Community
  • 1
  • 1
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • To expand a bit, I'd recommend you spend a bit of time learning about the RoleEntryPoint class. This is what is inheritted from in the defualt WorkerRole.cs file. I did a blog post on it awhile back: http://brentdacodemonkey.wordpress.com/2011/09/24/leveraging-the-roleentrypoint-year-of-azure-week-12/ – BrentDaCodeMonkey May 04 '12 at 12:16
2

Here is something you can use to understand Windows Azure Worker Role:

  1. Windows Azure is Platform as a Service and you will get to run your application in a dedicated virtual machine (except with extra-small instance)
  2. The application architecture for Windows Azure application supports 3 different kind of applications called Web Role, Worker Role and VM Role,

    2.1. A Web role is considered an application in which IIS is pre-configured and ready for your application. In most cases it is a web based application but it may not if you wish, but IIS will always be there. With IIS, you can run either an ASP.NET application or a node.js application it is your choice to decide what kind of application you would want.

    2.2. A Worker role is considered to be an application which does not need IIS and it is up to you whatever you would want to run on Worker role, C#, Java, PHP, Python or anything else. It is mainly used for open source web application or an application which perform actions as back-end and does not need web front end.

    2.3 VM Role is in BETA and used to run on a custom VHD deployed by user. We would consider it is in following explanation.

  3. All of these roles are actually a libraries means they compiled as DLL and when they run on Windows Azure, they actually needs a host process to run. For Web Role, the host process is WaWebHost.exe or WaWebIIS.exe, for WorkerRole the host process is WaWorkerHost.exe.

  4. When these host process starts in Windows Azure they look for a file call E:__entrypoint.txt which provided the Role DLL name and location so host process and find and load it.

  5. These Web and Worker role class are derived from RoleEntryPoint base class which extend all of required function for a web and worker role to run in Windows Azure Environment

  6. When you create a web or worker role using, Azure SDK template you will get these base code file where Web and Worker Role class can implement required functions. For Worker Role the call WorkerRole defined in WorkerRole.cs and for Web Role it is WebRole.cs.

  7. If you decided to add code specific to Windows Azure Runtime i.e. configuration or some setting you are going to add here because when role will start via host process, the code you have added in WebRole.cs or WorkerRole.cs will execute in Windows Azure runtime context.

AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65