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.

注意

本文所介紹的是雲端服務 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.

LanguagesLanguages

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# 公開推出 (.NET Framework 4.7)GA (.NET Framework 4.7) GA (.NET Core 2.2)GA (.NET Core 2.2)
JavaScriptJavaScript 公開推出 (Node 6)GA (Node 6) 公開推出 (Node 8 & 10)GA (Node 8 & 10)
F#F# 公開推出 (.NET Framework 4.7)GA (.NET Framework 4.7) GA (.NET Core 2.2)GA (.NET Core 2.2)
JavaJava N/AN/A GA (JAVA 8)GA (Java 8)
PowerShellPowerShell 實驗性Experimental 預覽 (PowerShell Core 6)Preview (PowerShell Core 6)
PythonPython 實驗性Experimental GA (Python 3.6. x)GA (Python 3.6.x)
TypeScriptTypeScript 實驗性Experimental GA (透過轉譯到 JavaScript 的支援)GA (supported through transpiling to JavaScript)
BashBash 實驗性Experimental N/AN/A
批次 (.cmd、.bat)Batch (.cmd, .bat) 實驗性Experimental N/AN/A
PHPPHP 實驗性Experimental N/AN/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".

  • 為了改進監視功能,已將入口網站中使用 AzureWebJobsDashboard 設定的 WebJobs 儀表板,取代成使用 APPINSIGHTS_INSTRUMENTATIONKEY 設定的 Azure Application Insights。To improve monitoring, the WebJobs dashboard in the portal, which used the AzureWebJobsDashboard setting is replaced with Azure Application Insights, which uses the APPINSIGHTS_INSTRUMENTATIONKEY setting. 如需詳細資訊,請參閱監視 Azure FunctionsFor more information, see Monitor Azure Functions.

  • 函數應用程式中的所有函式都必須共用相同語言。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.

  • App Service 方案中函式的預設逾時已變更為 30 分鐘。The default timeout for functions in an App Service plan is changed to 30 minutes. 您可以藉由在 host.json 中使用 functionTimeout 設定,將逾時手動變更回無限制。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.

  • 「事件方格」觸發程序 Webhook 的 URL 格式已變更為 https://{app}/runtime/webhooks/{triggerName}The URL format of Event Grid trigger webhooks has been changed to https://{app}/runtime/webhooks/{triggerName}.

移轉在本機開發的應用程式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.x 版Version 1.x
<TargetFramework>net461</TargetFramework>
<AzureFunctionsVersion>v1</AzureFunctionsVersion>
2.x 版Version 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 版進行開發,請安裝 2.x 版的 Core Tools。To develop against version 2.x, install version 2.x of the Core Tools. 1.x 版開發需要 1.x 版的 Core Tools。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

FUNCTIONS_EXTENSION_VERSION 應用程式設定會控制 Azure 中所發佈應用程式所使用的 Functions 執行階段版本。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 執行時間的兩個主要版本中支援的系結:This table shows the bindings that are supported in the two major versions of the Azure Functions runtime:

TypeType 1.x1.x 2.x12.x1 觸發程序Trigger 輸入Input OutputOutput
Blob 儲存體Blob storage
Cosmos DBCosmos DB
Event GridEvent Grid
事件中樞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
SignalRSignalR
資料表儲存體Table storage
計時器Timer
TwilioTwilio

1 在 2.x 中,必須註冊 HTTP 和計時器以外的所有繫結。1 In 2.x, all bindings except HTTP and Timer must be registered. 請參閱註冊繫結延伸模組See Register binding extensions.

函式應用程式逾時持續期間Function app timeout duration

FunctionTimeout 屬性中所定義的函式應用程式的逾時持續時間host.json專案檔。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 1010
耗用量Consumption 2.x2.x 55 1010
App Service 方案App Service 1.x1.x 無限Unlimited 無限Unlimited
App Service 方案App Service 2.x2.x 3030 無限Unlimited

注意

函式應用程式逾時設定,無論 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 Load Balancer 的閒置逾時This is because of the default idle timeout of Azure Load Balancer. 對於較長的處理時間,請考慮使用長期函式非同步模式或是延後實際工作,並傳回立即回應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: