Youngjae Youngjae - 1 year ago 113
C# Question

How to make continuous WebJob?

I create an Azure WebJob as shown in the following script which dequeues an item from Azure Storage Queue, and then stores it to an Azure Table. The process is finished running within 1 or 2 seconds, but it runs only few a times in a minute (and occasionally halted in approximately 10 minutes). Overall, it does not work well.

What am I missing? Maybe I'm mixing trigger job and continuous job, but it is hard to find an appropriate sample.

class Program
static void Main(string[] args)
Console.WriteLine("started at {0}", DateTime.Now.ToString("s"));

// Continuous job should have infinite loop.
var host = new JobHost();

public static void ProcessQueueMessage([QueueInput("blogqueue")] string json)
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings ["StorageConnectionString"].ConnectionString);
var tableClient = storageAccount.CreateCloudTableClient();

// Store it to Azure Table Storage
// omitted...

Console.WriteLine("Received JSON: {0}", json);

Answer Source

A few comments:

  1. You don't need the while(true) statement. RunAndBlock already has a while loop inside and it will block there.
  2. You don't need Manually retrieve the table. You can bind to a table as shown in the samples here
  3. You can bind to the storage account, you don't need to manually read the connection string from the configuration file and create the instance.

Here is an example of binding to storage account:

public static void ProcessQueueMessage([QueueInput("blogqueue")] string json, CloudStorageAccount) 

This is a wild guess but, from your code, it seems that the storage connection string is stored in StorageConnectionString in the config file. The JobHost expects either (1) the connection strings for runtime and data to be stored in AzureJobsData and AzureJobsRuntime (as described here) or (2) pass the connection string as parameter to the JobHost constructor. I think this is the reason of the crash.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download