A worker role is Azure's version of a Windows service. Worker roles are used for continual or long-running tasks, such as processing data held in an Azure queue (we will look at Azure queues shortly). Worker roles cannot be accessed directly like a web role or ASP.NET page, but they do allow the creation of HTTP-based endpoints for inter-role communication. Please see the Azure samples' November training kit (www.microsoft.com/downloads/details.aspx?FamilyID=413e88f8-5966-4a83-b309-53b7b77edf78&displaylang=en), which contains a thumbnail image generator example.
In many Azure projects, you will want to use both web and worker roles. To add a worker role to an existing project, just right-click the ~/Roles/ directory, and then select to add a new worker role. You may remember you also had the option to add a role when creating a project.
Let's take a quick look at worker roles:
Right-click the Roles directory, and select Add — New Worker Role Project.
Call it Chapter16.WorkerRole.
Open WorkerRole.cs if it is not already open, and you should see code similar to the following (shortened to save space). Note how a worker role at its most basic level is little more than a big loop for you to put your code inside.
public override void Run() { Trace.WriteLine("WorkerRole1 entry point called", "Information"); while (true) { Thread.Sleep(10000); Trace.WriteLine("Working", "Information"); } }
public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; DiagnosticMonitor.Start("DiagnosticsConnectionString"); RoleEnvironment.Changing += RoleEnvironmentChanging; return base.OnStart(); } }
18.224.67.58