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, …):