Azure Functions 运行时版本概述Azure Functions runtime versions overview

Azure Functions 运行时有两个主版本:1.x 和 2.x。There are two major versions of the Azure Functions runtime: 1.x and 2.x. 当前版本为 2.x,其中可使用新功能并在进行改进,但两个版本都支持生产方案。The current version where new feature work and improvements are being made is 2.x, though both are supported for production scenarios. 下面详细介绍了两个版本的一些区别、如何创建每个版本以及从 1.x 升级到 2.x。The following details some of the differences between the two, how you can create each version, and upgrade from 1.x to 2.x.

Note

本文引用了云服务 Azure Functions。This article refers to the cloud service Azure Functions. 如需深入了解可运行本地 Azure Functions 的预览产品,请参阅 Azure Functions 运行时概述For information about the preview product that lets you run Azure Functions on-premises, see the Azure Functions Runtime Overview.

跨平台开发Cross-platform development

2.x 版运行时在 .NET Core 2 上运行,因此,它可以在 .NET Core 支持的所有平台(包括 macOS 和 Linux)上运行。The version 2.x runtime runs on .NET Core 2, which enables it to run on all platforms supported by .NET Core, including macOS and Linux. 在 .NET Core 上运行可以实现跨平台开发和托管方案。Running on .NET Core enables cross-platform development and hosting scenarios.

相比之下,1.x 版运行时仅支持 Azure 门户或 Windows 计算机上的开发和托管。By comparison, the version 1.x runtime only supports development and hosting in the Azure portal or on Windows computers.

语言Languages

2.x 版运行时使用新的语言扩展性模型。The version 2.x runtime uses a new language extensibility model. 在版本 2.x 中,函数应用中的所有函数必须共享相同的语言。In version 2.x, all functions in a function app must share the same language. 函数应用中的函数的语言是在创建应用时选择的。The language of functions in a function app is chosen when creating the app.

Azure Functions 1.x 试验性语言不会更新为使用新模型,因此它们在 2.x 中不受支持。Azure Functions 1.x experimental languages won't be updated to use the new model, so they aren't supported in 2.x. 下表指示每个运行时版本目前支持的编程语言。The following table indicates which programming languages are currently supported in each runtime version.

语言Language 1.x1.x 2.x2.x
C#C# GA (.NET Framework 4.7)GA (.NET Framework 4.7) GA (.NET Core 2)GA (.NET Core 2)
JavaScriptJavaScript GA (Node 6)GA (Node 6) GA(Node 8 和 10)GA (Node 8 & 10)
F#F# GA (.NET Framework 4.7)GA (.NET Framework 4.7) GA (.NET Core 2)GA (.NET Core 2)
JavaJava 不适用N/A GA (Java 8)GA (Java 8)
PythonPython 试验Experimental 预览版 (Python 3.6)Preview (Python 3.6)
TypeScriptTypeScript 试验Experimental 正式版(通过转换为 JavaScript 进行支持)GA (supported through transpiling to JavaScript)
PHPPHP 试验Experimental 不适用N/A
批处理(.cmd、.bat)Batch (.cmd, .bat) 试验Experimental 不适用N/A
BashBash 试验Experimental 不适用N/A
PowerShellPowerShell 试验Experimental 不适用N/A

有关语言支持计划更改的信息,请参阅 Azure 路线图For information about planned changes to language support, see Azure roadmap.

有关详细信息,请参阅支持的语言For more information, see Supported languages.

在版本 1.x 上运行Run on version 1.x

默认情况下,在 Azure 门户中创建的函数应用设置为版本 2.x。By default, function apps created in the Azure portal are set to version 2.x. 应尽可能地使用此运行时版本,因为其中包含投资开发的新功能。When possible, you should use this runtime version, where new feature investments are being made. 如果需要,仍可在版本 1.x 运行时中运行函数应用。If you need to, you can still run a function app on the version 1.x runtime. 只能在创建函数应用之后、添加任何函数之前更改运行时版本。You can only change the runtime version after you create your function app but before you add any functions. 若要了解如何将运行时版本固定为 1.x,请参阅查看和更新当前运行时版本To learn how to pin the runtime version to 1.x, see View and update the current runtime version.

从 1.x 迁移到 2.xMigrating from 1.x to 2.x

可以选择迁移所编写的现有应用,以使用 1.x 版运行时,而不使用版本 2.x。You may choose to migrate an existing app written to use the version 1.x runtime to instead use version 2.x. 需要做出的大多数更改与语言运行时的更改相关,例如,.NET Framework 4.7 与 .NET Core 2 之间的 C# API 更改。Most of the changes you need to make are related to changes in the language runtime, such as C# API changes between .NET Framework 4.7 and .NET Core 2. 还需要确保代码和库与所选的语言运行时兼容。You'll also need to make sure your code and libraries are compatible with the language runtime you choose. 最后,请务必注意触发器、绑定和以下突出显示功能中的任何更改。Finally, be sure to note any changes in trigger, bindings, and features highlighted below. 为获得最佳迁移结果,应该为版本 2.x 创建一个新函数应用,并将现有的 1.x 版函数代码移植到新应用。For the best migration results, you should create a new function app for version 2.x and port your existing version 1.x function code to the new app.

触发器和绑定的更改Changes in triggers and bindings

版本 2.x 要求为应用中的函数所用的特定触发器和绑定安装扩展。Version 2.x requires you to install the extensions for specific triggers and bindings used by the functions in your app. 唯一的例外是 HTTP 和计时器触发器,它们不需要扩展。The only exception for this HTTP and timer triggers, which don't require an extension. 有关详细信息,请参阅注册和安装绑定扩展For more information, see Register and install binding extensions.

此外,在不同的版本中,函数的 function.json 或属性存在几处更改。There have also been a few changes in the function.json or attributes of the function between versions. 例如,事件中心的 path 属性现在为 eventHubNameFor example, the Event Hub path property is now eventHubName. 请参阅现有绑定表,以获取每个绑定的文档链接。See the existing binding table for links to documentation for each binding.

特性和功能的更改Changes in features and functionality

此外,新版本中已删除、更新或替换了几项功能。A few features that have also been removed, updated, or replaced in the new version. 本部分详细介绍在使用版本 1.x 后,版本 2.x 中会出现的更改。This section details the changes you see in version 2.x after having used version 1.x.

在版本 2.x 中做出了以下更改:In version 2.x, the following changes were made:

  • 用于调用 HTTP 终结点的密钥始终以加密方式存储在 Azure Blob 存储中。Keys for calling HTTP endpoints are always stored encrypted in Azure Blob storage. 在版本 1.x 中,密钥默认存储在 Azure 文件存储中。In version 1.x, keys were stored in Azure File storage be default. 将应用从版本 1.x 升级到版本 2.x 时,会重置文件存储中的现有机密。When upgrading an app from version 1.x to version 2.x, existing secrets that are in file storage are reset.

  • 2.x 版运行时不包含对 Webhook 提供程序的内置支持。The version 2.x runtime doesn't include built-in support for webhook providers. 做出此项更改的目的是提高性能。This change was made to improve performance. 仍可以使用 HTTP 触发器作为 Webhook 的终结点。You can still use HTTP triggers as endpoints for webhooks.

  • 主机配置文件 (host.json) 应该为空或包含字符串 "version": "2.0"The host configuration file (host.json) should be empty or have the string "version": "2.0".

  • 函数应用中的所有函数必须共享相同的语言。All functions in a function app must share the same language. 创建函数应用时,必须选择该应用的运行时堆栈。When you create a function app, you must choose a runtime stack for the app. 运行时堆栈由应用程序设置中的 FUNCTIONS_WORKER_RUNTIME 值指定。The runtime stack is specified by the FUNCTIONS_WORKER_RUNTIME value in application settings. 增加此项要求的目的是减少占用空间和启动时间。This requirement was added to improve footprint and startup time. 进行本地开发时,还必须在 local.settings.json 文件中包含此设置。When developing locally, you must also include this setting in the local.settings.json file.

  • 应用服务计划中函数的默认超时已更改为 30 分钟。The default timeout for functions in an App Service plan is changed to 30 minutes. 可以使用 host.json 中的 functionTimeout 设置,将超时手动改回到 unlimited(无限)。You can manually change the timeout back to unlimited by using the functionTimeout setting in host.json.

  • 默认情况下,将对消耗计划函数实施 HTTP 并发性限制,每个实例的并发请求数默认为 100。HTTP concurrency throttles are implemented by default for consumption plan functions, with a default of 100 concurrent requests per instance. 可以在 host.json 文件中的 maxConcurrentRequests 设置内更改此值。You can change this in the maxConcurrentRequests setting in the host.json file.

  • 由于 .NET Core 的限制,已删除对 F# 脚本 (.fsx) 函数的支持。Because of .NET core limitations, support for F# script (.fsx) functions has been removed. 编译的 F# 函数 (.fs) 仍受支持。Compiled F# functions (.fs) are still supported.

迁移本地开发的应用程序Migrating a locally developed application

现有的函数应用项目可能是使用 1.x 版运行时在本地开发的。You may have existing function app projects that you developed locally using the version 1.x runtime. 若要升级到版本 2.x,应该针对版本 2.x 创建一个本地函数应用项目,并将现有代码移植到新应用。To upgrade to version 2.x, you should create a local function app project against version 2.x and port your existing code into the new app. 可以手动更新现有的项目和代码,这是一种“就地”升级的方法。You could manually update the existing project and code, a sort of "in-place" upgrade. 但是,在版本 1.x 与版本 2.x 之间,可能还需要做出其他一些改进。However, there are a number of other improvements between version 1.x and version 2.x that you may still need to make. 例如,在 C# 中,调试对象已从 TraceWriter 更改为 ILoggerFor example, in C# the debugging object was changed from TraceWriter to ILogger. 创建新的 2.x 版项目后,可以基于最新的 2.x 版模板,从更新的函数着手进行开发。By creating a new version 2.x project, you start off with updated functions based on the latest version 2.x templates.

Visual Studio 运行时版本Visual Studio runtime versions

在 Visual Studio 中,可在创建项目时选择运行时版本。In Visual Studio, you select the runtime version when you create a project. 用于 Visual Studio 的 Azure Functions 工具支持这两个主要运行时版本。Azure Functions tools for Visual Studio supports both major runtime versions. 基于项目设置进行调试和发布时,将使用正确的版本。The correct version is used when debugging and publishing based on project settings. 版本设置在 .csproj 文件中的以下属性内定义:The version settings are defined in the .csproj file in the following properties:

版本 1.xVersion 1.x
<TargetFramework>net461</TargetFramework>
<AzureFunctionsVersion>v1</AzureFunctionsVersion>
版本 2.xVersion 2.x
<TargetFramework>netcoreapp2.2</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>

调试或发布项目时,将使用正确的运行时版本。When you debug or publish your project, the correct version of the runtime is used.

VS Code 和 Azure Functions Core ToolsVS Code and Azure Functions Core Tools

Azure Functions Core Tools 可用于命令行开发,另外,还可供适用于 Visual Studio Code 的 Azure Functions 扩展使用。Azure Functions Core Tools is used for command line development and also by the Azure Functions extension for Visual Studio Code. 若要针对版本 2.x 进行开发,请安装 Core Tools 版本 2.x。To develop against version 2.x, install version 2.x of the Core Tools. 版本 1.x 开发需要 Core Tools 版本 1.x。Version 1.x development requires version 1.x of the Core Tools. 有关详细信息,请参阅安装 Azure Functions Core ToolsFor more information, see Install the Azure Functions Core Tools.

对于 Visual Studio Code 开发,可能还需要更新 azureFunctions.projectRuntime 的用户设置,使之与已安装工具的版本匹配。For Visual Studio Code development, you may also need to update the user setting for the azureFunctions.projectRuntime to match the version of the tools installed. 此设置还会更新创建函数应用期间使用的模板和语言。This setting also updates the templates and languages used during function app creation.

在 Azure 中更改应用版本Changing version of apps in Azure

Azure 中的已发布应用使用的 Functions 运行时版本由 FUNCTIONS_EXTENSION_VERSION 应用程序设置指定。The version of the Functions runtime used by published apps in Azure is dictated by the FUNCTIONS_EXTENSION_VERSION application setting. ~2 表示面向 2.x 版运行时,~1 表示面向 1.x 版运行时。A value of ~2 targets the version 2.x runtime and ~1 targets the version 1.x runtime. 请不要随意更改此设置,因为这可能需要在函数中进行其他应用设置更改和代码更改。Don't arbitrarily change this setting, because other app setting changes and code changes in your functions are likely required. 若要了解将函数应用迁移到不同运行时版本的建议方法,请参阅如何以 Azure Functions 运行时版本为目标To learn about the recommended way to migrate your function app to a different runtime version, see How to target Azure Functions runtime versions.

绑定Bindings

2.x 版运行时使用新的绑定扩展性模型,该模型具有以下优势:The version 2.x runtime uses a new binding extensibility model that offers these advantages:

  • 支持第三方绑定扩展。Support for third-party binding extensions.

  • 运行时和绑定分离。Decoupling of runtime and bindings. 此项更改允许对绑定扩展进行版本控制和单独发布操作。This change allows binding extensions to be versioned and released independently. 例如,可以选择升级到依赖于基础 SDK 的较新版本的扩展版本。You can, for example, opt to upgrade to a version of an extension that relies on a newer version of an underlying SDK.

  • 更轻便的执行环境,其中运行时仅知道和加载正在使用的绑定。A lighter execution environment, where only the bindings in use are known and loaded by the runtime.

除 HTTP 和计时器触发器外,其他所有绑定必须显式添加到函数应用项目,或者在门户中注册。With the exception of HTTP and timer triggers, all bindings must be explicitly added to the function app project, or registered in the portal. 有关详细信息,请参阅注册绑定扩展For more information, see Register binding extensions.

下表显示了每个运行时版本支持的绑定。The following table shows which bindings are supported in each runtime version.

下表显示了 Azure Functions 运行时的两个主要版本支持的绑定。The following table shows the bindings that are supported in the two major versions of the Azure Functions runtime.

类型Type 1.x1.x 2.x12.x1 触发器Trigger 输入Input 输出Output
Blob 存储Blob Storage
Cosmos DBCosmos DB
事件中心Event Hubs
HTTP 和 WebhookHTTP & Webhooks
Microsoft Graph
Excel 表
Microsoft Graph
Excel tables
Microsoft Graph
OneDrive 文件
Microsoft Graph
OneDrive files
Microsoft Graph
Outlook 电子邮件
Microsoft Graph
Outlook email
Microsoft Graph
事件
Microsoft Graph
Events
Microsoft Graph
身份验证令牌
Microsoft Graph
Auth tokens
移动应用Mobile Apps
通知中心Notification Hubs
队列存储Queue storage
SendGridSendGrid
服务总线Service Bus
表存储Table storage
计时器Timer

1 在 2.x 中,除了 HTTP 和 Timer 以外,所有绑定都必须注册。1 In 2.x, all bindings except HTTP and Timer must be registered. 请参阅注册绑定扩展See Register binding extensions.

函数应用超时持续时间Function app timeout duration

函数应用的超时持续时间可通过 host.json 项目文件中的 functionTimeout 属性定义。The timeout duration of a function app is defined by the functionTimeout property in the host.json project file. 下表显示两种计划和两种运行时版本的默认值和最大值(以分钟为单位):The following table shows the default and maximum values in minutes for both plans and in both runtime versions:

计划Plan 运行时版本Runtime Version 默认Default 最大值Maximum
消耗Consumption 1.x1.x 55 10 个10
消耗Consumption 2.x2.x 55 10 个10
应用服务App Service 1.x1.x 无限制Unlimited 无限制Unlimited
应用服务App Service 2.x2.x 3030 无限制Unlimited

Note

不管函数应用超时设置如何,230 秒是 HTTP 触发的函数在响应请求时需要的最长时间。Regardless of the function app timeout setting, 230 seconds is the maximum amount of time that an HTTP triggered function can take to respond to a request. 这起因于 Azure 负载均衡器的默认空闲超时This is because of the default idle timeout of Azure Load Balancer. 对于处理时间较长的情况,考虑使用 Durable Functions 异步模式延迟实际工作并返回即时响应For longer processing times, consider using the Durable Functions async pattern or defer the actual work and return an immediate response.

后续步骤Next steps

有关详细信息,请参阅以下资源:For more information, see the following resources: