如何面向 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.x 和 3.xThere are three major versions: 1.x, 2.x, and 3.x. 默认情况下,函数应用采用 3.x 版的运行时创建。By default, function apps are created in version 3.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. 如果仅指定主版本,当运行时的新次要版本可用时,函数应用将自动更新到该版本。If you specify only the major version, the function app is automatically updated to new minor versions of the runtime when they become available. 新的次要版本不应引入中断性变更。New minor versions shouldn't 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. 会定期从生产环境中删除旧的次要版本。Older minor versions are regularly removed from the production environment. 发生这种情况后,函数应用将在最新版本上运行,而不是在 FUNCTIONS_EXTENSION_VERSION 中设置的版本上运行。After this occurs, your function app runs on the latest version instead of the version set in FUNCTIONS_EXTENSION_VERSION. 因此,应快速解决需要特定次要版本来解决的任何函数应用问题,以便可以专注使用主要版本。Because of this, you should quickly resolve any issues with your function app that require a specific minor version, so that you can instead target the major version. 应用服务公告中会公布次要版本删除信息。Minor version removals are announced in App Service announcements.

备注

如果你固定到 Azure Functions 的特定主要版本,然后尝试使用 Visual Studio 发布到 Azure,则会弹出一个对话框窗口,提示你更新到最新版本或取消发布。If you pin to a specific major 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. 若要避免出现此情况,请在 .csproj 文件中添加 <DisableFunctionExtensionVersionUpdate>true</DisableFunctionExtensionVersionUpdate> 属性。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.

下表显示了启用自动更新所需的每个主版本的 FUNCTIONS_EXTENSION_VERSION 值:The following table shows the FUNCTIONS_EXTENSION_VERSION values for each major version to enable automatic updates:

主版本Major version FUNCTIONS_EXTENSION_VERSIONFUNCTIONS_EXTENSION_VERSION value
3.x3.x ~3
2.x2.x ~2
1.x1.x ~1

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

查看和更新当前运行时版本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 can 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.

使用以下步骤查看并更新函数应用当前使用的运行时版本。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 Settings, choose Configuration. 在“函数运行设置”选项卡中,定位“运行时版本” 。In the Function runtime settings tab, locate the Runtime version. 请注意特定的运行时版本。Note the specific runtime version. 在以下示例中,版本设置为 ~3In the example below, the version is set to ~3.

    查看运行时版本。

  3. 若要将函数应用固定到 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.

  4. 如果更改了运行时版本,请返回到“概览” 选项卡并选择“重启” 以重启应用。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.

在对应用程序设置进行更改后,函数应用将重启。The function app restarts after the change is made to the application setting.

后续步骤Next steps