Background thread in Azure Web Role Background thread in Azure Web Role azure azure

Background thread in Azure Web Role


I would suggets a solution that is different from Leon and David's solution:

  • David's solution is OK but is not resilient. What it the instance/process goes offline while processing the task?
  • Leon's solution mostly applies to scheduled jobs, but sending an email isn't always something that is scheduled (maybe you want to send an email when someone registers in your app).

An other option you should look at is using Windows Azure Storage Queues (they are very cheap) in this scenario:

  • Your web application: Sends messages to the queue (like 'send an email to someone@someone.com')
  • WebRole.cs: spawn a new thread when starting the instance and have it listen for messages from that queue. Whenever an message arrives, process it. If success, remove the message from the queue.

This solution has many advantages. The WebRole.cs runs in a different process than your web application, so there is no impact on the request threads. And besides that, if sending the mail fails for whatever reason, the message will stay in the queue and will be processed the next time. This will make sure you won't loose any tasks to execute if the application or the process crashes.

Here is an example to get you started. Note that you'll need to improve this code if you want it to be production ready (retry policy, exception handling, backoff polling, ...):

using System;using System.Collections.Generic;using System.Linq;using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.Diagnostics;using Microsoft.WindowsAzure.ServiceRuntime;using Microsoft.WindowsAzure.StorageClient;using System.Threading.Tasks;namespace MvcWebRole1{    public class WebRole : RoleEntryPoint    {        public override bool OnStart()        {            Task.Factory.StartNew(InitializeQueueListener);            return base.OnStart();        }        private void InitializeQueueListener()        {            Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>            {                configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));            });            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");            var queueStorage = storageAccount.CreateCloudQueueClient();            var queue = queueStorage.GetQueueReference("myqueue");            queue.CreateIfNotExist();            while (true)            {                CloudQueueMessage msg = queue.GetMessage();                if (msg != null)                {                    // DO SOMETHING HERE                    queue.DeleteMessage(msg);                }                else                {                    System.Threading.Thread.Sleep(1000);                }            }        }    }}


Absolutely you can create a thread (or lots of them). A Web Role is basically Windows 2008 Server. You don't need a separate Worker Role just to set up a background task. Of course, you can have a separate worker role, which would allow you to scale those instances independently of your Web role instances. This is where you'll need to balance performance/scaling with cost.