Background thread in Azure Web Role
azure, azure-web-roles, c#, multithreading
Solution
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);
}
}
}
}
}
Problem
It is possible to run a thread from the WebRole.cs OnStart() method in such a way that we are able to access it through aspx page to perform background work? I know that the correct way to do it would be to use a Worker Role but i wish to maintain the running costs as low as possible. The idea would be to create a thread that would be always running and waiting for a job, for instance if i want to make a blocking operation like sending an email i would use the thread giving the SendEmail method, is it possible to do? If so, can you provide me some examples that could point me in the right direction?