Close

Creating a webjob in Azure App Services

I wanted to create a webjob in Azure that was running on a schedule once per day. Information on how to do that can be easily found online. I created a console application.

In this post I want to share some gotchas I found out when creating the webjob. First you have to create a website manually, because a web job always has to be hosted within a website. Then you can publish the web job to Azure, but unfortunately I ran into an issue when I wanted to view the log file via the SCM website:

Make sure that you are setting a connection string named AzureWebJobsDashboard in your Microsoft Azure Website configuration by using the following format DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY pointing to the Microsoft Azure Storage account where the Microsoft Azure WebJobs Runtime logs are stored. I found out that you have to create a storage account via the Classic Azure Portal instead of storage account created via Azure Preview Portal. When you create this account, for instance via the Server Explorer in Visual Studio, you can retrieve the primary access key.

This access key has to be entered in two places:

  • Web App / App Settings / ConnectionString -> Add connection string for AzureWebJobsStorage, for instance: DefaultEndpointsProtocol=https;AccountName=appservicesstoragedev;AccountKey=<key>.
  • First of all note that you don’t use Name and Key (like the error states), but AccountName and AccountKey. Secondly remember to select Custom for the type of connection string.
  • Webjob console application, app.config:<add name=”AzureWebJobsStorage” connectionString=”DefaultEndpointsProtocol=https;AccountName=appservicesstoragedev;AccountKey=<key>” />
  • <add name=”AzureWebJobsDashboard” connectionString=”DefaultEndpointsProtocol=https;AccountName=appservicesstoragedev;AccountKey=<key>” />

For debugging purposes it’s a good idea to run the webjob locally first. Right-click webjob and select Debug / Start New Instance. After debugging the webjob I found out that I got an error when invoking the function via host.Call(typeof(Program).GetMethod(“ArchiveData”));

I got the following error.
Solution to “can’t be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?”

The webjopb is not trigger via a queue or blob message entry. It’s triggered on a schedule, so I added Azure WebJobs SDK attribute NoAutomaticTrigger.

[NoAutomaticTrigger()] Public static void RunJob(TextWriter log) {}

After making this change, it still didn’t work. Then I found the containing class of the console app didn’t have a public accessor:

I changed: class Program instead of public class Program

The total solution looks like below:

public class Program
{
static void Main()
{
var host = new JobHost();
host.Call(typeof(Program).GetMethod(“ArchiveData”));
}

[NoAutomaticTrigger()] public static void ArchiveData()
{  }

So now I can run the webjob locally. I still get errors when running the webjob via the Azure Preview Portal. What I noticed is that my web job is On Demand, not Scheduled. That’s strange, because webjobs-publish-settings.json in my console application is showing Scheduled:

{
“$schema”: “http://schemastore.org/schemas/json/webjob-publish-settings.json”,
“webJobName”: “Archiving”,
“startTime”: “2015-11-17T00:00:00+01:00”,
“endTime”: “2015-11-18T00:00:00+01:00”,
“jobRecurrenceFrequency”: “Day”,
“interval”: 1,
“runMode”: “Scheduled”
}

After I did Publish as Azure WebJobs from Visual Studio, the job became “On Demand” in the Azure Preview Portal. That’s strange. This is what I found on internet: Currently the Azure Preview Portal doesn’t support scheduled WebJobs. So you can’t create a scheduled WebJob there and it will not recognize a WebJob that has a scheduler job associated with it. So for now please use the classic Azure portal for managing scheduled WebJobs. A second important note. When you deploy a webjob, don’t set the schedule interval to let’s say 15 minutes. In that case you will receive an error “The remote server returned an error: (409) Conflict”. Just set the schedule interval to one day. After publishing, go to the classic Azure Portal and change the minimum interval on the Scale tab to 15 minutes and then change the scheduling interval for the webjob.

In the Azure Portal, I could see the webjob, but not the history. The webjob is scheduled, so that’s OK. What I had to do, is change the SCALE from free to premium. When I now run the webjob manually via the Azure Preview Portal, I can see the call succeeds. Not sure if the job succeeds because I changed the scale to Premium, but that’s the only thing I changed.

Note. The result of the webjob execution can still be viewed in the Azure Preview Portal only. I still get the error message for the AzureWebJobsDashboard, but I can actually view the history.