MultiThreadedWorkerRole project

The MultiThreadedWorkerRole project is the worker role in the AzureProviders solution.

Multi-threaded role

The MultiThreadedWorkerRole project's job is to spawn a new thread for each job that is done. If you create a standard Azure worker role only a single thread is in use, this is a huge waste of resources as the server instances that the worker roles run on have processors with many cores that are capable of processing many threads asynchronously, MultiThreadedWorkerRole worker role counters his by spawning one thread for each role.

See for information on how this multi-threaded worker role software design pattern works.

Back-off pattern

The MultiThreadedWorkerRole project inherits a Back-Off design pattern from ThreadedWorkerRoleLib, this pattern causes the System.Threading.Thread.Sleep() to double after each time the job was unemployed. Since you pay per processing hour on Azure this design pattern saves you money.

Since some jobs are more important than others you can specify the maximum back-off time. As an example, sending an e-mail immediately may not be so important, the user gladly can wait a minute, but charging someone's credit card should happen immediately.


app.config is an application configuration file.


  • NumberOfSendEmailRetries; (default=3) Number of send an e-mail attempts before giving up
  • CheckDeadThreadSleepInSeconds; (default=4) Number of seconds to sleep after checking that all the worker role threads are up and running
  • QueueDequeueRetrySleepSeconds; (default=3600) Number of seconds to wait before retrying if a message was dequeued
  • EmailWorkerMaxThreadSleepSeconds; (default=30) When using the Back Off pattern never let the e-mail worker thread sleep more than this number of seconds
  • SessionStateWorkerMaxThreadSleepSeconds; (default=60) When using the Back Off pattern never let the session-state worker thread sleep more than this number of seconds
  • ApplicationName; (default="MyApp1") Name of application, should match the one in the WebPages web.config


Contains the E-mail SMTP server settings. You must change this to your own e-mail server provider, if you do not have one the you can use SendGrid ( which allows you to send up to 200 e-mails a day for free.


By default ASP.NET only allows 2 concurrent client requests, this is really inefficient as your Azure server instances has capabilities of processing many more simultaneous asynchronous requests. Because of this AzureProviders accepts 48 concurrent client requests by default, but you should really try and find the setting that works best for your solution. See for more information on the subject.


By default ASP.NET uses the HTTP Expect 100-Continue pattern, which can be a performance issue if your HTTP operations are small, because of this AzureProviders has turned HTTP Expect 100-Continue off. See for more information on the subject.


The EmailWorker class listens to the Azure e-mail queue for e-mails that is to be sent. EmailWorker will try to send the e-mail as many times as specified in the application setting NumberOfSendEmailRetries before giving up.

Since EmailWorker inherits WorkerEntryPoint it will run in its own thread.

SMTP server

The SMTP settings is found in the mailSettings section of app.config.


The MessageWorker class is an example of how a minimal job is defined in AzureProviders. A job defined like this would run in its own thread.

Since MessageWorker inherits WorkerEntryPoint it will run in its own thread.


The SessionStateWorker class clears any expired ASP.NET Membership sessions from the SessionStateEntity table.

Since SessionStateWorker inherits WorkerEntryPoint it will run in its own thread.


The WorkerRole class is the entry point for the Azure infrastructure process, it initializes the role process.

The WorkerRole.GetWorkerRoles() method is where you define which jobs you want to run in the MultiThreadedWorkerRole worker role, by default the EmailWorker and SessionStateWorker jobds are defined here, you can add your own here. All jobds will run in their own thread, separate from the other roles in the MultiThreadedWorkerRole worker role. You can comment in MessageWorker to experiment with adding a new job.

WorkerRole inherits the abstract ThreadedWorkerRoleLib.ThreadedRoleEntryPoint which handles most of the logic for the job.

EmailWorkerMaxThreadSleepSeconds and SessionStateWorkerMaxThreadSleepSeconds settings in app.config sets the maximum time to thread sleep using the Back-Off pattern.

Last edited Oct 2, 2011 at 2:30 PM by ihenriksen, version 5


No comments yet.