The WebJob is very simple.

  1. Create a new Console  Application in Visual Studio
  2. Open the Package Manager Console and enter the following command:
    Install-Package Microsoft.WindowsAzure.Jobs.Host -pre
  3. Replace the Main method with the code that starts the host. (See below.)
  4. With the help of the WebJob SDK you add the attribute [QueueIntput] to the parameter in the method header. The parameter sendoutqueue is the name of the queue that the job will listen to.
class Program
{
  static void Main(string[] args)
  {
    JobHost host = newJobHost();
    host.RunAndBlock();
  }

  public static void WaitForMessageInQueue([QueueInput("sendoutqueue")]
                                           Email email)
  {
    Console.WriteLine("Send email to {0}", email.To);
  }
}

The Email class is created in a separate project called Common, so I can share it between the test project that creates messages and the webjob project that receives these messages.

namespace Common
{
  public class Email
  {
    public string To { get; set; }
    public string Message { get; set; }
  }
}

The WebJobs SDK requires two Azure Storage connection strings to work. I put them in App.Config.

<?xml version="1.0"encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <addname="AzureJobsRuntime" connectionString="DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=ACCOUNT_KEY"/>
    <addname="AzureJobsData" connectionString="DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=ACCOUNT_KEY"/>
  </connectionStrings>
[...]
</configuration>

Now we are ready to upload and install this webjob in Azure Web Sites. Here is a description how to do that. (You do not need to upload it to Azure. You can test it on your own computer if you want.)

Creating a message

You can create a message on a queue in several ways. I will show how to do this in code. This code is in my example implemented in a console application, but you can of course implemented in a web application instead.

  1. Create a new Console Application
  2. Add the nuget package for Windows Azure Storage
  3. Add the following code inside the Main method.
static void Main(string[] args)
{
  CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

  CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
  CloudQueue queue = queueClient.GetQueueReference("sendoutqueue");

  queue.CreateIfNotExists();

  var email = newEmail()
  {
    To = "kalle@anka.se",
    Message = "Hello"
  };

  queue.AddMessage(newCloudQueueMessage(JsonConvert.SerializeObject(email)));
}
  1. Add the Azure Storage connection string to App.config:
<?xmlversion="1.0"encoding="utf-8" ?>
  <configuration>
    <appSettings>
    <addkey="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=ACCOUNT_KEY" />
    </appSettings>
[...]
</configuration>
  1. Run it and a new message will be created on the queue sendoutqueue

Here is the source code for this example