您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

如何面向 Azure Functions 运行时版本How to target Azure Functions runtime versions

函数应用在特定版本的 Azure Functions 运行时上运行。A function app runs on a specific version of the Azure Functions runtime. 有两个主版本:1.x 和 2.xThere are two major versions: 1.x and 2.x. 默认情况下,函数应用基于 2.x 版的运行时创建。By default, function apps that are created version 2.x of the runtime. 本文介绍如何在 Azure 中将函数应用配置为在所选的版本上运行。This article explains how to configure a function app in Azure to run on the version you choose. 有关如何为特定版本配置本地开发环境的信息,请参阅在本地编码和测试 Azure FunctionsFor information about how to configure a local development environment for a specific version, see Code and test Azure Functions locally.

自动和手动版本更新Automatic and manual version updates

Azure Functions,可通过使用面向特定版本的运行时FUNCTIONS_EXTENSION_VERSION函数应用中的应用程序设置。Azure Functions lets you target a specific version of the runtime by using the FUNCTIONS_EXTENSION_VERSION application setting in a function app. 函数应用将一直保留在指定的主版本上,直到显式选择迁移到新版本。The function app is kept on the specified major version until you explicitly choose to move to a new version.

如果仅指定主版本(对于 2.x 为“~2”;对于 1.x 为“~1”),当运行时的新次要版本可用时,函数应用将自动更新到该版本。If you specify only the major version ("~2" for 2.x or "~1" for 1.x), the function app is automatically updated to new minor versions of the runtime when they become available. 新次要版本不会引入重大更改。New minor versions do not introduce breaking changes. 如果指定次要版本(例如,“2.0.12345”),则函数应用将被固定到该特定版本,直到显式更改它。If you specify a minor version (for example, "2.0.12345"), the function app is pinned to that specific version until you explicitly change it.

备注

如果您将固定到特定版本的 Azure Functions 中,然后再尝试发布到 Azure 中使用 Visual Studio,将弹出一个对话框窗口提示您更新到最新版本或取消发布。If you pin to a specific version of Azure Functions, and then try to publish to Azure using Visual Studio, a dialog window will pop up prompting you to update to the latest version or cancel the publish. 若要避免此问题,将添加<DisableFunctionExtensionVersionUpdate>true</DisableFunctionExtensionVersionUpdate>中的属性在.csproj文件。To avoid this, add the <DisableFunctionExtensionVersionUpdate>true</DisableFunctionExtensionVersionUpdate> property in your .csproj file.

有新版本公开可用时,门户中将显示提示,让你可以升级到该版本。When a new version is publicly available, a prompt in the portal gives you the chance to move up to that version. 升级至新版本后,始终可使用 FUNCTIONS_EXTENSION_VERSION 应用程序设置移回到之前的版本。After moving to a new version, you can always use the FUNCTIONS_EXTENSION_VERSION application setting to move back to a previous version.

更改运行时版本会导致函数应用重启。A change to the runtime version causes a function app to restart.

可以在 FUNCTIONS_EXTENSION_VERSION 应用设置中为 1.x 运行时 启用自动更新设置的值当前为“~1”,为 2.x 运行时 启用自动更新设置的值为“~2”。The values you can set in the FUNCTIONS_EXTENSION_VERSION app setting to enable automatic updates are currently "~1" for the 1.x runtime and "~2" for 2.x.

查看和更新当前运行时版本View and update the current runtime version

你可以使用函数应用的运行时版本。You can change the runtime version used by your function app. 由于可能会成为重大更改,因此只应在函数应用中创建任何函数之前更改运行时版本。Because of the potential of breaking changes, you should only change the runtime version before you have created any functions in your function app. 虽然运行时版本由 FUNCTIONS_EXTENSION_VERSION 设置决定,但你应该在 Azure 门户中进行此更改,而不是直接更改设置。Although the runtime version is determined by the FUNCTIONS_EXTENSION_VERSION setting, you should make this change in the Azure portal and not by changing the setting directly. 这是因为该门户会验证你的更改,并根据需要进行其他相关更改。This is because the portal validates your changes and makes other related changes as needed.

通过 Azure 门户From the Azure portal

使用以下步骤查看并更新函数应用当前使用的运行时版本。Use the following procedure to view and update the runtime version currently used by a function app.

  1. Azure 门户中,浏览到你的函数应用。In the Azure portal, browse to your function app.

  2. 在“配置的功能” 下,选择“函数应用设置”。 Under Configured Features, choose Function app settings.

    选择函数应用设置

  3. 在“函数应用设置” 选项卡中,定位“运行时版本” 。In the Function app settings tab, locate the Runtime version. 请注意特定运行时版本和请求的主版本。Note the specific runtime version and the requested major version. 在以下示例中,版本设置为 ~2In the example below, the version is set to ~2.

    选择函数应用设置

  4. 若要将函数应用固定到 1.x 版运行时,请在“运行时版本”下选择“~1”。 To pin your function app to the version 1.x runtime, choose ~1 under Runtime version. 当应用中存在函数时,此开关被禁用。This switch is disabled when you have functions in your app.

  5. 如果更改了运行时版本,请返回到“概览” 选项卡并选择“重启” 以重启应用。When you change the runtime version, go back to the Overview tab and choose Restart to restart the app. 函数应用重新开始在 1.x 版运行时上运行,并且在创建函数时将使用 1.x 版模板。The function app restarts running on the version 1.x runtime, and the version 1.x templates are used when you create functions.

备注

使用 Azure 门户,您不能更改已包含函数的函数应用的运行时版本。Using the Azure portal, you can't change the runtime version for a function app that already contains functions.

通过 Azure CLIFrom the Azure CLI

还可以通过 Azure CLI 查看和更新 FUNCTIONS_EXTENSION_VERSIONYou can also view and set the FUNCTIONS_EXTENSION_VERSION from the Azure CLI.

备注

因为运行时版本可能会影响其他设置,因此,应当在门户中更改版本。Because other settings may be impacted by the runtime version, you should change the version in the portal. 当你更改运行时版本时,门户会自动进行其他必需的更新,例如 Node.js 版本和运行时堆栈。The portal automatically makes the other needed updates, such as Node.js version and runtime stack, when you change runtime versions.

使用 Azure CLI 时,可以使用 az functionapp config appsettings set 命令查看当前的运行时版本。Using the Azure CLI, view the current runtime version with the az functionapp config appsettings set command.

az functionapp config appsettings list --name <function_app> \
--resource-group <my_resource_group>

在此代码中,用函数应用名称替代 <function_app>In this code, replace <function_app> with the name of your function app. 此外,还使用函数应用的资源组名称替代 <my_resource_group>Also replace <my_resource_group> with the name of the resource group for your function app.

在以下输出中,你会看到 FUNCTIONS_EXTENSION_VERSION,为了清晰起见,该输出已被截断:You see the FUNCTIONS_EXTENSION_VERSION in the following output, which has been truncated for clarity:

[
  {
    "name": "FUNCTIONS_EXTENSION_VERSION",
    "slotSetting": false,
    "value": "~2"
  },
  {
    "name": "FUNCTIONS_WORKER_RUNTIME",
    "slotSetting": false,
    "value": "dotnet"
  },
  
  ...
  
  {
    "name": "WEBSITE_NODE_DEFAULT_VERSION",
    "slotSetting": false,
    "value": "8.11.1"
  }
]

可以使用 az functionapp config appsettings set 命令更新函数应用中的 FUNCTIONS_EXTENSION_VERSION 设置。You can update the FUNCTIONS_EXTENSION_VERSION setting in the function app with the az functionapp config appsettings set command.

az functionapp config appsettings set --name <function_app> \
--resource-group <my_resource_group> \
--settings FUNCTIONS_EXTENSION_VERSION=<version>

<function_app> 替换为你的函数应用的名称。Replace <function_app> with the name of your function app. 此外,还使用函数应用的资源组名称替代 <my_resource_group>Also replace <my_resource_group> with the name of the resource group for your function app. 另外,将 <version> 替换为 1.x 运行时的有效版本或版本 2.x 的 ~2Also, replace <version> with a valid version of the 1.x runtime or ~2 for version 2.x.

可以通过在前面代码示例中选择“试一试” 运行这个来自 Azure Cloud Shell 的命令。You can run this command from the Azure Cloud Shell by choosing Try it in the preceding code sample. 还可以在执行 az login 登录后使用 Azure CLI 在本地执行此命令。You can also use the Azure CLI locally to execute this command after executing az login to sign in.

后续步骤Next steps