使用 Visual Studio 开发和部署 WebJobs - Azure 应用服务Develop and deploy WebJobs using Visual Studio - Azure App Service

本文介绍如何使用 Visual Studio 将控制台应用程序项目作为 Azure WebJob 部署到应用服务中的 Web 应用。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. 有关如何使用 Azure 门户部署 WebJobs 的信息,请参阅使用 WebJobs 运行后台任务For information about how to deploy WebJobs by using the Azure portal, see Run Background tasks with WebJobs.

可将多个 WebJob 发布到单个 Web 应用。You can publish multiple WebJobs to a single web app. 请确保 Web 应用中的每个 WebJob 具有唯一的名称。Make sure that each WebJob in a web app has a unique name.

Azure WebJobs SDK 版本 3.x 可用于开发作为 .NET Core 应用或 .NET Framework 应用运行的 WebJobs,而版本 2.x 仅支持 .NET Framework。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. 部署 WebJobs 项目的方式因该项目是 .NET Core 项目还是 .NET Framework 项目而有所不同。The way that you deploy a WebJobs project is different for .NET Core projects versus .NET Framework ones.

用作 .NET Core 控制台应用的 WebJobWebJobs as .NET Core console apps

使用 WebJobs 版本 3.x 时,可以创建并发布用作 .NET Core 控制台应用的 WebJob。When using version 3.x of the WebJobs, you can create and publish WebJobs as .NET Core console apps. 有关创建 .NET Core 控制台应用程序并将其作为 WebJob 发布到 Azure 的分步说明,请参阅用于事件驱动式后台处理的 Azure WebJobs SDK 入门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 WebJob 无法与 Web 项目相链接。.NET Core WebJobs cannot be linked with web projects. 如果需要将 WebJob 与 Web 应用一起部署,应该创建用作 .NET Framework 控制台应用的 WebJobIf you need to deploy your WebJob with a web app, you should create your WebJob as a .NET Framework console app.

部署到 Azure 应用服务Deploy to Azure App Service

通过 Visual Studio 将 .NET Core WebJob 发布到应用服务所用的工具,与发布 ASP.NET Core 应用所用的工具相同。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. 在“发布”对话框中,依次选择“Microsoft Azure 应用服务”和“新建”,然后选择“发布”。In the Publish dialog, select Microsoft Azure App Service, choose Create New, and then select Publish.

    选取发布目标

  3. 在“创建应用服务”对话框中,使用图片下方的表中指定的托管设置:In the Create App Service dialog, use the hosting settings as specified in the table below the image:

    “创建应用服务”对话框

    设置Setting 建议的值Suggested value 说明Description
    应用名称App Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。Name that uniquely identifies your new function app.
    订阅Subscription 选择订阅Choose your subscription 要使用的 Azure 订阅。The Azure subscription to use.
    资源组Resource Group MyResourceGroupmyResourceGroup 要在其中创建 Function App 的资源组的名称。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 应用服务计划指定托管应用的 Web 服务器场的位置、大小和功能。An App Service plan specifies the location, size, and features of the web server farm that hosts your app. 可以在托管多个应用时节省资金,方法是将多个 Web 应用配置为共用单个应用服务计划。You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan. 应用服务计划定义区域、实例大小、规模计数和 SKU(免费、共享、基本、标准或高级)。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. 单击“创建”以使用这些设置在 Azure 中创建 Web 作业和相关资源,并部署项目代码。Click Create to create a WebJob and related resources in Azure with these settings and deploy your project code.

Web 作业类型WebJob types

默认情况下,从 .NET Core 控制台项目发布的 WebJob 只会在触发后才会运行,或者按需运行。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

Web 应用可在进入非活动状态 20 分钟后超时。A web app can time out after 20 minutes of inactivity. 只有向实际 Web 应用发出的请求才会重置计时器。Only requests to the actual web app reset the timer. 在 Azure 门户中查看应用的配置或向高级工具站点 (https://<app_name>.scm.chinacloudsites.cn) 发出请求不会重置计时器。Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://<app_name>.scm.chinacloudsites.cn) don't reset the timer. 如果应用运行连续或计划的 Web 作业,可启用 Always On 来确保 Web 作业可靠运行。If your app runs continuous or scheduled 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

将 .NET Core 控制台应用程序发布到 Azure 时,会将一个新的 settings.job 文件添加到项目。When you publish a .NET Core console application to Azure, a new settings.job file is added to the project. 使用此文件可以设置 WebJob 的执行计划。Use this file to set an execution schedule for your WebJob. 有关详细信息,请参阅计划触发的 WebJobFor more information, see Scheduling a triggered WebJob.

连续执行Continuous execution

可以使用 Visual Studio 来更改 WebJob,以便在 Azure 中启用 Always On 后连续运行该 WebJob。You can use Visual Studio to change the WebJob to run continuously when Always On is enabled in Azure.

  1. 将项目发布到 Azure(如果尚未这样做)。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. 在“配置文件设置”对话框中,为“WebJob 类型”选择“连续”,然后选择“保存”。 In the Profile Settings dialog, choose Continuous for WebJob Type, and choose Save.

    WebJob 的“发布设置”对话框

  5. 选择“发布”,以使用更新的设置重新发布 WebJob。 Select Publish to republish the WebJob with the updated settings.

用作 .NET Framework 控制台应用的 WebJobWebJobs as .NET Framework console apps

当 Visual Studio 部署启用 WebJobs 的 .NET Framework 控制台应用程序项目时,它会将运行时文件复制到 Web 应用中的相应文件夹(对于连续运行的 WebJobs,该文件夹为 App_Data/jobs/continuous,对于按计划运行或按需运行的 WebJobs,该文件夹为 App_Data/jobs/triggered)。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).

已启用 Web 作业的项目中添加了以下项:A WebJobs-enabled project has the following items added to it:

显示在控制台应用中添加哪些项目才能让部署作为 Web 作业的插图

可以将这些项添加到现有控制台应用程序项目,或使用模板创建已启用 Web 作业的新控制台应用程序项目。You can add these items to an existing Console Application project or use a template to create a new WebJobs-enabled Console Application project.

以 Web 作业的方式部署项目,或者将项目链接到 Web 项目,因此每要部署 Web 项目时,项目便会自动部署。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. 为了链接项目,Visual Studio 会在 Web 项目的 webjobs-list.json 文件中包含启用 Web 作业的项目的名称。To link projects, Visual Studio includes the name of the WebJobs-enabled project in a webjobs-list.json file in the web project.

显示链接到 Web 项目的 Web 作业项目的插图

先决条件Prerequisites

如果使用的是 Visual Studio 2015,请安装用于 .NET 的 Azure SDK (Visual Studio 2015)If you're using Visual Studio 2015, install the Azure SDK for .NET (Visual Studio 2015).

如果使用的是 Visual Studio 2019,请安装 Azure 开发工作负荷If you're using Visual Studio 2019, install the Azure development workload.

为现有控制台应用程序项目启用 WebJobs 部署Enable WebJobs deployment for an existing Console Application project

可以使用两个选项:You have two options:

  • 使用 Web 项目启用自动部署Enable automatic deployment with a web project.

    配置现有控制台应用程序项目,以便在部署 Web 项目时,它以 Web 作业的方式自动部署。Configure an existing Console Application project so that it automatically deploys as a WebJob when you deploy a web project. 要在运行相关 Web 应用程序的同一 Web 应用中运行 Web 作业时,请使用此选项。Use this option when you want to run your WebJob in the same web app in which you run the related web application.

  • 不使用 Web 项目启用部署Enable deployment without a web project.

    配置现有控制台应用程序项目以 Web 作业的方式部署,且不提供 Web 项目的链接。Configure an existing Console Application project to deploy as a WebJob by itself, with no link to a web project. 要在 Web 应用中运行 Web 作业,且 Web 应用中没有正在运行的 Web 应用程序时,请使用此选项。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. 为了能够缩放不受 Web 应用程序资源影响的 Web 作业资源,可能要执行此操作。You might want to do this in order to be able to scale your WebJob resources independently of your web application resources.

  1. 右键单击“解决方案资源管理器” 中的 Web 项目,并依次单击“添加” > “用作 Azure Web 作业的现有项目” 。Right-click the web project in Solution Explorer, and then click Add > Existing Project as Azure WebJob.

    用作 Azure Web 作业的现有项目

    此时显示添加 Azure Web 作业对话框。The Add Azure WebJob dialog box appears.

  2. 在“项目名称” 下拉列表中,选择要添加为 Web 作业的控制台应用程序项目。In the Project name drop-down list, select the Console Application project to add as a WebJob.

    在“添加 Azure Web 作业”对话框中选择项目

  3. 完成添加 Azure Web 作业对话框,并单击“确定” 。Complete the Add Azure WebJob dialog, and then click OK.

  1. 右键单击“解决方案资源管理器”中的控制台应用程序项目,然后单击“发布为 Azure Web 作业” 。Right-click the Console Application project in Solution Explorer, and then click Publish as Azure WebJob....

    发布为 Azure Web 作业

    此时显示添加 Azure Web 作业对话框,其“项目名称” 框中已选中该项目。The Add Azure WebJob dialog box appears, with the project selected in the Project name box.

  2. 完成添加 Azure Web 作业对话框,并单击“确定” 。Complete the Add Azure WebJob dialog box, and then click OK.

    此时显示“发布 Web” 向导。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.

创建已启用 Web 作业的新项目Create a new WebJobs-enabled project

若要创建已启用 Web 作业的新项目,可以使用控制台应用程序项目模板,并根据 上一节所述启用 Web 作业部署。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. 或者,可以使用 Web 作业新建项目模板:As an alternative, you can use the WebJobs new-project template:

  • 为独立的 Web 作业使用 Web 作业新建项目模板Use the WebJobs new-project template for an independent WebJob

    创建一个项目,并将它配置为以 Web 作业的方式部署,且不提供 Web 项目的链接。Create a project and configure it to deploy by itself as a WebJob, with no link to a web project. 要在 Web 应用中运行 Web 作业,且 Web 应用中没有正在运行的 Web 应用程序时,请使用此选项。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. 为了能够缩放不受 Web 应用程序资源影响的 Web 作业资源,可能要执行此操作。You might want to do this in order to be able to scale your WebJob resources independently of your web application resources.

  • 在链接到 Web 项目的 Web 作业中使用 Web 作业新建项目模板Use the WebJobs new-project template for a WebJob linked to a web project

    创建一个项目,该项目配置为在针对位于相同解决方案中的 Web 项目进行部署时,自动以 Web 作业的方式部署。Create a project that is configured to deploy automatically as a WebJob when a web project in the same solution is deployed. 要在运行相关 Web 应用程序的同一 Web 应用中运行 Web 作业时,请使用此选项。Use this option when you want to run your WebJob in the same web app in which you run the related web application.

Note

Web 作业新建项目模板会自动安装 NuGet 包,并在 Program.cs 中包含适用于 WebJobs SDK 的代码。The WebJobs new-project template automatically installs NuGet packages and includes code in Program.cs for the WebJobs SDK. 如果不想使用 WebJobs SDK,请删除或更改 Program.cs 中的 host.RunAndBlock 语句 。If you don't want to use the WebJobs SDK, remove or change the host.RunAndBlock statement in Program.cs.

  1. 依次单击“文件” > “新建项目”,然后在“新建项目”对话框中,依次单击“云” > “Azure Web 作业 (.NET Framework)” 。Click File > New Project, and then in the New Project dialog box click Cloud > Azure WebJob (.NET Framework).

    显示了 Web 作业模板的“新建项目”对话框

  2. 按照前述说明, 将控制台应用程序项目设为独立的 Web 作业项目Follow the directions shown earlier to make the Console Application project an independent WebJobs project.

  1. 右键单击“解决方案资源管理器” 中的 Web 项目,并依次单击“添加” > “新建 Azure Web 作业项目” 。Right-click the web project in Solution Explorer, and then click Add > New Azure WebJob Project.

    “新建 Azure Web 作业项目”菜单项

    此时显示添加 Azure Web 作业对话框。The Add Azure WebJob dialog box appears.

  2. 完成“添加 Azure Web 作业”对话框,并单击“确定” 。Complete the Add Azure WebJob dialog box, and then click OK.

添加 Azure Web 作业对话框The Add Azure WebJob dialog

可在“添加 Azure Web 作业”对话框中输入 Web 作业的名称和 Web 作业的运行模式设置 。The Add Azure WebJob dialog lets you enter the WebJob name and run mode setting for your WebJob.

“添加 Azure Web 作业”对话框

此对话框中的字段对应于 Azure 门户的“添加 WebJob” 对话框中的字段。The fields in this dialog correspond to fields on the Add WebJob dialog of the Azure portal. 有关详细信息,请参阅使用 Web 作业运行后台任务For more information, see Run Background tasks with WebJobs.

Note

  • 有关命令行部署的信息,请参阅启用 Azure Web 作业的命令行或连续传送For information about command-line deployment, see Enabling Command-line or Continuous Delivery of Azure WebJobs.
  • 如果部署了某个 WebJob,但后来想要更改该 WebJob 的类型并重新部署,则需要删除 webjobs-publish-settings.json 文件。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. 这样,Visual Studio 就会再次显示发布选项,使你可以更改 Web 作业的类型。This will make Visual Studio show the publishing options again, so you can change the type of WebJob.
  • 如果部署了某个 Web 作业,并随后将运行模式从连续更改为非连续(或相反),则在重新部署时,Visual Studio 将在 Azure 中创建新的 Web 作业。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. 如果更改了其他计划设置但保持运行模式不变,或在计划模式与按需模式之间切换,则 Visual Studio 会更新现有的作业,而不是创建新的作业。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.jsonwebjob-publish-settings.json

设置 Web 作业部署的控制台应用程序时,Visual Studio 会安装 Microsoft.Web.WebJobs.Publish NuGet 包,并将计划信息存储在 Web 作业项目的项目 Properties 文件夹中的 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"
    }

可以编辑此文件目录,Visual Studio 会提供 IntelliSense。You can edit this file directly, and Visual Studio provides IntelliSense. https://schemastore.org 中可查看存储的文件架构。The file schema is stored at https://schemastore.org and can be viewed there.

webjobs-list.jsonwebjobs-list.json

如果将已启用 Web 作业的项目链接到 Web 项目,Visual Studio 会将 Web 作业项目的名称存储在 Web 项目 Properties 文件夹的 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. 该列表可能包含多个 Web 作业项目,如以下示例所示: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"
        }
      ]
    }

可以编辑此文件目录,Visual Studio 会提供 IntelliSense。You can edit this file directly, and Visual Studio provides IntelliSense. https://schemastore.org 中可查看存储的文件架构。The file schema is stored at https://schemastore.org and can be viewed there.

部署 Web 作业项目Deploy a WebJobs project

已链接到 Web 项目的 Web 作业项目会通过 Web 项目自动部署。A WebJobs project that you have linked to a web project deploys automatically with the web project. 有关 Web 项目部署的信息,请参阅左侧导航区域中的“操作指南” > “部署应用” 。For information about web project deployment, see How-to guides > Deploy app in the left navigation.

若要自动部署某个 Web 作业项目,请在“解决方案资源管理器”中右键单击该项目,然后单击“发布为 Azure Web 作业” 。To deploy a WebJobs project by itself, right-click the project in Solution Explorer and click Publish as Azure WebJob....

发布为 Azure Web 作业

对于独立的 Web 作业,会显示 Web 项目使用的相同“发布 Web” 向导,但其中的可更改设置更少。For an independent WebJob, the same Publish Web wizard that is used for web projects appears, but with fewer settings available to change.

计划触发的 WebJobScheduling a triggered WebJob

WebJobs 使用 settings.job 文件确定某个 WebJob 是否已运行。WebJobs uses a settings.job file to determine when a WebJob is run. 使用此文件可以设置 WebJob 的执行计划。Use this file to set an execution schedule for your WebJob. 以下示例从上午 9 点到下午 5 点每隔一小时运行:The following example runs every hour from 9 AM to 5 PM:

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

此文件必须位于 WebJobs 文件夹的根目录下,与 WebJob 的脚本(例如 wwwroot\app_data\jobs\triggered\{job name}wwwroot\app_data\jobs\continuous\{job name})放在一起。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}. 从 Visual Studio 部署 Web 作业时,请将 settings.job 文件属性标记为“如果较新则复制”。 When you deploy a WebJob from Visual Studio, mark your settings.job file properties as Copy if newer.

从 Azure 门户创建 WebJob 时,系统会自动创建 settings.job 文件。When you create a WebJob from the Azure portal, the settings.job file is created for you.

Note

Web 应用可在进入非活动状态 20 分钟后超时。A web app can time out after 20 minutes of inactivity. 只有向实际 Web 应用发出的请求才会重置计时器。Only requests to the actual web app reset the timer. 在 Azure 门户中查看应用的配置或向高级工具站点 (https://<app_name>.scm.chinacloudsites.cn) 发出请求不会重置计时器。Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://<app_name>.scm.chinacloudsites.cn) don't reset the timer. 如果应用运行连续或计划的 Web 作业,可启用 Always On 来确保 Web 作业可靠运行。If your app runs continuous or scheduled 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 表达式CRON expressions

WebJobs 使用的 CRON 计划表达式与 Azure Functions 中的计时器触发器相同。WebJobs uses the same CRON expressions for scheduling as the timer trigger in Azure Functions. 若要详细了解 CRON 支持,请参阅计时器触发器参考文章To learn more about CRON support, see the timer trigger reference article.

setting.job 参考setting.job reference

WebJobs 支持以下设置: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. 有关详细信息,请参阅 WebJobs 工作目录To learn more, see WebJobs working directory.
is_singleton 连续Continuous 仅在横向扩展的单个实例上运行 WebJob。有关详细信息,请参阅将连续作业设为单一实例Only run the WebJobs on a single instance when scaled out. To learn more, see Set a continuous job as singleton.
schedule 触发Triggered 根据基于 CRON 的计划运行 WebJob。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