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?
I would suggets a solution that is different from Leon and David's solution:
An other option you should look at is using Windows Azure Storage Queues (they are very cheap) in this scenario:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With