Develop and deploy WebJobs using Visual Studio - Azure App Service

This article explains how to use Visual Studio to deploy a Console Application project to a web app in App Service as an Azure WebJob. For information about how to deploy WebJobs by using the Azure portal, see Run Background tasks with WebJobs.

You can publish multiple WebJobs to a single web app. Make sure that each WebJob in a web app has a unique name.

Version 3.x of the Azure WebJobs SDK lets you develop WebJobs that run as either .NET Core apps or .NET Framework apps, while version 2.x supports only the .NET Framework. The way that you deploy a WebJobs project is different for .NET Core projects versus .NET Framework ones.

WebJobs as .NET Core console apps

When using version 3.x of the WebJobs, you can create and publish WebJobs as .NET Core console apps. For step-by-step instructions to create and publish a .NET Core console application to Azure as a WebJob, see Get started with the Azure WebJobs SDK for event-driven background processing.

Note

.NET Core WebJobs cannot be linked with web projects. If you need to deploy your WebJob with a web app, you should create your WebJob as a .NET Framework console app.

Deploy to Azure App Service

Publishing a .NET Core WebJob to App Service from Visual Studio uses the same tooling as publishing an ASP.NET Core app.

  1. In Solution Explorer, right-click the project and select Publish.

  2. In the Publish dialog, select Microsoft Azure App Service, choose Create New, and then select Publish.

    Pick publish target

  3. In the Create App Service dialog, use the hosting settings as specified in the table below the image:

    Create App Service dialog

    Setting Suggested value Description
    App Name Globally unique name Name that uniquely identifies your new function app.
    Subscription Choose your subscription The Azure subscription to use.
    Resource Group myResourceGroup Name of the resource group in which to create your function app. Choose New to create a new resource group.
    Hosting Plan App Service plan An App Service plan specifies the location, size, and features of the web server farm that hosts your app. You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan. App Service plans define the region, instance size, scale count, and SKU (Free, Shared, Basic, Standard, or Premium). Choose New to create a new App Service plan.
  4. Click Create to create a WebJob and related resources in Azure with these settings and deploy your project code.

WebJob types

By default, a WebJob published from a .NET Core console project runs only when triggered or on demand. You can also update the project to run on a schedule or run continuously.

Note

A web app can time out after 20 minutes of inactivity. Only requests to the actual web app reset the timer. Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://<app_name>.scm.azurewebsites.net) don't reset the timer. If your app runs continuous or scheduled (Timer trigger) WebJobs, enable Always On to ensure that the WebJobs run reliably. This feature is available only in the Basic, Standard, and Premium pricing tiers.

Scheduled execution

When you publish a .NET Core console application to Azure, a new settings.job file is added to the project. Use this file to set an execution schedule for your WebJob. For more information, see Scheduling a triggered WebJob.

Continuous execution

You can use Visual Studio to change the WebJob to run continuously when Always On is enabled in Azure.

  1. If you haven't already done so, publish the project to Azure.

  2. In Solution Explorer, right-click the project and select Publish.

  3. In the Publish tab, choose Settings.

  4. In the Profile Settings dialog, choose Continuous for WebJob Type, and choose Save.

    Publish Settings dialog for a WebJob

  5. Select Publish to republish the WebJob with the updated settings.

WebJobs as .NET Framework console apps

When Visual Studio deploys a WebJobs-enabled .NET Framework Console Application project, it copies runtime files to the appropriate folder in the web app (App_Data/jobs/continuous for continuous WebJobs and App_Data/jobs/triggered for scheduled or on-demand WebJobs).

A WebJobs-enabled project has the following items added to it:

Diagram showing what is added to a Console App to enable deployment as a WebJob

You can add these items to an existing Console Application project or use a template to create a new WebJobs-enabled Console Application project.

You can deploy a project as a WebJob by itself, or link it to a web project so that it automatically deploys whenever you deploy the web project. To link projects, Visual Studio includes the name of the WebJobs-enabled project in a webjobs-list.json file in the web project.

Diagram showing WebJob project linking to web project

Prerequisites

If you're using Visual Studio 2015, install the Azure SDK for .NET (Visual Studio 2015).

If you're using Visual Studio 2017, install the Azure development workload.

Enable WebJobs deployment for an existing Console Application project

You have two options:

  • Enable automatic deployment with a web project.

    Configure an existing Console Application project so that it automatically deploys as a WebJob when you deploy a web project. Use this option when you want to run your WebJob in the same web app in which you run the related web application.

  • Enable deployment without a web project.

    Configure an existing Console Application project to deploy as a WebJob by itself, with no link to a web project. Use this option when you want to run a WebJob in a web app by itself, with no web application running in the web app. You might want to do this in order to be able to scale your WebJob resources independently of your web application resources.

  1. Right-click the web project in Solution Explorer, and then click Add > Existing Project as Azure WebJob.

    Existing Project as Azure WebJob

    The Add Azure WebJob dialog box appears.

  2. In the Project name drop-down list, select the Console Application project to add as a WebJob.

    Selecting project in Add Azure WebJob dialog

  3. Complete the Add Azure WebJob dialog, and then click OK.

  1. Right-click the Console Application project in Solution Explorer, and then click Publish as Azure WebJob....

    Publish as Azure WebJob

    The Add Azure WebJob dialog box appears, with the project selected in the Project name box.

  2. Complete the Add Azure WebJob dialog box, and then click OK.

    The Publish Web wizard appears. If you do not want to publish immediately, close the wizard. The settings that you've entered are saved for when you do want to deploy the project.

Create a new WebJobs-enabled project

To create a new WebJobs-enabled project, you can use the Console Application project template and enable WebJobs deployment as explained in the previous section. As an alternative, you can use the WebJobs new-project template:

  • Use the WebJobs new-project template for an independent WebJob

    Create a project and configure it to deploy by itself as a WebJob, with no link to a web project. Use this option when you want to run a WebJob in a web app by itself, with no web application running in the web app. You might want to do this in order to be able to scale your WebJob resources independently of your web application resources.

  • Use the WebJobs new-project template for a WebJob linked to a web project

    Create a project that is configured to deploy automatically as a WebJob when a web project in the same solution is deployed. Use this option when you want to run your WebJob in the same web app in which you run the related web application.

Note

The WebJobs new-project template automatically installs NuGet packages and includes code in Program.cs for the WebJobs SDK. If you don't want to use the WebJobs SDK, remove or change the host.RunAndBlock statement in Program.cs.

  1. Click File > New Project, and then in the New Project dialog box click Cloud > Azure WebJob (.NET Framework).

    New Project dialog showing WebJob template

  2. Follow the directions shown earlier to make the Console Application project an independent WebJobs project.

  1. Right-click the web project in Solution Explorer, and then click Add > New Azure WebJob Project.

    New Azure WebJob Project menu entry

    The Add Azure WebJob dialog box appears.

  2. Complete the Add Azure WebJob dialog box, and then click OK.

The Add Azure WebJob dialog

The Add Azure WebJob dialog lets you enter the WebJob name and run mode setting for your WebJob.

Add Azure WebJob dialog

The fields in this dialog correspond to fields on the Add WebJob dialog of the Azure portal. For more information, see Run Background tasks with WebJobs.

Note

  • For information about command-line deployment, see Enabling Command-line or Continuous Delivery of Azure WebJobs.
  • If you deploy a WebJob and then decide you want to change the type of WebJob and redeploy, you'll need to delete the webjobs-publish-settings.json file. This will make Visual Studio show the publishing options again, so you can change the type of WebJob.
  • If you deploy a WebJob and later change the run mode from continuous to non-continuous or vice versa, Visual Studio creates a new WebJob in Azure when you redeploy. If you change other scheduling settings but leave run mode the same or switch between Scheduled and On Demand, Visual Studio updates the existing job rather than create a new one.

webjob-publish-settings.json

When you configure a Console Application for WebJobs deployment, Visual Studio installs the Microsoft.Web.WebJobs.Publish NuGet package and stores scheduling information in a webjob-publish-settings.json file in the project Properties folder of the WebJobs project. Here is an example of that file:

    {
      "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
      "webJobName": "WebJob1",
      "startTime": "null",
      "endTime": "null",
      "jobRecurrenceFrequency": "null",
      "interval": null,
      "runMode": "Continuous"
    }

You can edit this file directly, and Visual Studio provides IntelliSense. The file schema is stored at https://schemastore.org and can be viewed there.

webjobs-list.json

When you link a WebJobs-enabled project to a web project, Visual Studio stores the name of the WebJobs project in a webjobs-list.json file in the web project's Properties folder. The list might contain multiple WebJobs projects, as shown in the following example:

    {
      "$schema": "http://schemastore.org/schemas/json/webjobs-list.json",
      "WebJobs": [
        {
          "filePath": "../ConsoleApplication1/ConsoleApplication1.csproj"
        },
        {
          "filePath": "../WebJob1/WebJob1.csproj"
        }
      ]
    }

You can edit this file directly, and Visual Studio provides IntelliSense. The file schema is stored at https://schemastore.org and can be viewed there.

Deploy a WebJobs project

A WebJobs project that you have linked to a web project deploys automatically with the web project. For information about web project deployment, see How-to guides > Deploy app in the left navigation.

To deploy a WebJobs project by itself, right-click the project in Solution Explorer and click Publish as Azure WebJob....

Publish as Azure WebJob

For an independent WebJob, the same Publish Web wizard that is used for web projects appears, but with fewer settings available to change.

Scheduling a triggered WebJob

WebJobs uses a settings.job file to determine when a WebJob is run. Use this file to set an execution schedule for your WebJob. The following example runs every hour from 9 AM to 5 PM:

{
    "schedule": "0 0 9-17 * * *"
}

This file must be located at the root of the WebJobs folder, along side your WebJob's script, such as wwwroot\app_data\jobs\triggered\{job name} or wwwroot\app_data\jobs\continuous\{job name}. When you deploy a WebJob from Visual Studio, mark your settings.job file properties as Copy if newer.

When you create a WebJob from the Azure portal, the settings.job file is created for you.

Note

A web app can time out after 20 minutes of inactivity. Only requests to the actual web app reset the timer. Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://<app_name>.scm.azurewebsites.net) don't reset the timer. If your app runs continuous or scheduled (Timer trigger) WebJobs, enable Always On to ensure that the WebJobs run reliably. This feature is available only in the Basic, Standard, and Premium pricing tiers.

CRON expressions

WebJobs uses the same CRON expressions for scheduling as the timer trigger in Azure Functions. To learn more about CRON support, see the timer trigger reference article.

setting.job reference

The following settings are supported by WebJobs:

Setting Type Description
is_in_place All Allows the job to run in place without being first copied to a temp folder. To learn more, see WebJobs working directory.
is_singleton Continuous Only run the WebJobs on a single instance when scaled out. To learn more, see Set a continuous job as singleton.
schedule Triggered Run the WebJob on a CRON-based schedule. TO learn more, see the timer trigger reference article.
stopping_wait_time All Allows control of the shutdown behavior. To learn more, see Graceful shutdown.

Next steps