Azure Functions 開發人員指南Azure Functions developers guide

在 Azure Functions 中,不論您使用何種語言或繫結,特定函式都會共用一些核心技術概念和元件。In Azure Functions, specific functions share a few core technical concepts and components, regardless of the language or binding you use. 閱讀指定語言或繫結特有的詳細資料之前,請務必詳閱這份適用於所有語言或繫結的概觀。Before you jump into learning details specific to a given language or binding, be sure to read through this overview that applies to all of them.

本文假設您已閱讀過 Azure Functions 開發人員概觀This article assumes that you've already read the Azure Functions overview.

函式程式碼Function code

「函數」 是 Azure Functions 中的主要概念。A function is the primary concept in Azure Functions. 函式包含兩個重要的部分,一個部分是可用各種語言撰寫的程式碼,另一則是一些組態,亦即 function.json 檔案。A function contains two important pieces - your code, which can be written in a variety of languages, and some config, the function.json file. 對於已編譯的語言,系統會自動從您程式碼中的註解產生此組態檔。For compiled languages, this config file is generated automatically from annotations in your code. 對於指令碼語言,您必須自行提供組態檔。For scripting languages, you must provide the config file yourself.

function.json 檔案會定義函式的觸發程序、繫結和其他組態設定。The function.json file defines the function's trigger, bindings, and other configuration settings. 每個函式會有一個且只能有一個觸發程序。Every function has one and only one trigger. 執行階段使用此組態檔來判斷要監視的事件,以及如何傳入資料並從函式執行傳回資料。The runtime uses this config file to determine the events to monitor and how to pass data into and return data from a function execution. 以下是範例 function.json 檔案。The following is an example function.json file.

{
    "disabled":false,
    "bindings":[
        // ... bindings here
        {
            "type": "bindingType",
            "direction": "in",
            "name": "myParamName",
            // ... more depending on binding
        }
    ]
}

如需詳細資訊,請參閱 Azure Functions 觸發程序和繫結概念For more information, see Azure Functions triggers and bindings concepts.

bindings 屬性可讓您設定觸發程序和繫結。The bindings property is where you configure both triggers and bindings. 每個繫結都共用一些共用設定,以及特定類別的繫結特有的一些設定。Each binding shares a few common settings and some settings which are specific to a particular type of binding. 每個繫結都需要下列設定︰Every binding requires the following settings:

屬性Property 值/類型Values/Types 註解Comments
type stringstring 繫結類型。Binding type. 例如: queueTriggerFor example, queueTrigger.
direction 'in'、'out''in', 'out' 表示繫結用於將資料接收到函數,還是從函數傳送資料。Indicates whether the binding is for receiving data into the function or sending data from the function.
name stringstring 用於函式中所繫結資料的名稱。The name that is used for the bound data in the function. 在 C# 中,這是引數名稱;在 JavaScript 中,這是索引鍵/值清單中的索引鍵。For C#, this is an argument name; for JavaScript, it's the key in a key/value list.

函式應用程式Function app

函數應用程式在 Azure 中提供您的函式可在其中執行的執行內容。A function app provides an execution context in Azure in which your functions run. 因此,這是您的函式部署和管理的單位。As such, it is the unit of deployment and management for your functions. 函式應用程式是由一或多個個別函式所組成,這些函式可一起管理、部署與調整。A function app is comprised of one or more individual functions that are managed, deployed, and scaled together. 函數應用程式中的所有函式會共用相同的定價方案、部署方法和執行階段版本。All of the functions in a function app share the same pricing plan, deployment method, and runtime version. 請將函式應用程式視為用來組織及集體管理函式的方式。Think of a function app as a way to organize and collectively manage your functions. 若要深入瞭解,請參閱如何管理函數應用程式To learn more, see How to manage a function app.

注意

函式應用程式中的所有函式都必須以相同的語言撰寫。All functions in a function app must be authored in the same language. 舊版的 Azure Functions 執行階段中,這不是必要的。In previous versions of the Azure Functions runtime, this wasn't required.

資料夾結構Folder structure

特定函式應用程式中所有函式的程式碼,都位於包含主機設定檔和一或多個子資料夾的根專案資料夾中。The code for all the functions in a specific function app is located in a root project folder that contains a host configuration file and one or more subfolders. 每個子資料夾都包含個別函式的程式碼。Each subfolder contains the code for a separate function. 資料夾結構會顯示在下列標記法中:The folder structure is shown in the following representation:

FunctionApp
 | - host.json
 | - MyFirstFunction
 | | - function.json
 | | - ...  
 | - MySecondFunction
 | | - function.json
 | | - ...  
 | - SharedCode
 | - bin

在2.x 版和更高版本的函式執行時間中,函數應用程式中的所有函式都必須共用相同的語言堆疊。In version 2.x and higher of the Functions runtime, all functions in the function app must share the same language stack.

主機. json檔案包含執行時間專屬的設定,而且位於函式應用程式的根資料夾中。The host.json file contains runtime-specific configurations and is in the root folder of the function app. Bin資料夾包含函數應用程式所需的套件和其他程式庫檔案。A bin folder contains packages and other library files that the function app requires. 請參閱函式應用程式專案以了解特定語言的需求:See the language-specific requirements for a function app project:

以上是 Function 應用程式的預設 (且建議的) 資料夾結構。The above is the default (and recommended) folder structure for a Function app. 如果您想要變更函式程式碼的檔案位置,請修改 function.json 檔案的 scriptFile 區段。If you wish to change the file location of a function's code, modify the scriptFile section of the function.json file. 我們也建議您使用套件部署來將您的專案部署到 Azure 中的函數應用程式中。We also recommend using package deployment to deploy your project to your function app in Azure. 您也可以使用諸如持續整合與部署和 Azure DevOps 之類的現有工具。You can also use existing tools like continuous integration and deployment and Azure DevOps.

注意

如果手動部署封裝,請務必將您的 host.json 檔案和函式資料夾直接部署至 wwwroot 資料夾。If deploying a package manually, make sure to deploy your host.json file and function folders directly to the wwwroot folder. 您的部署中請勿包含 wwwroot 資料夾。Do not include the wwwroot folder in your deployments. 否則,您會得到 wwwroot\wwwroot 資料夾。Otherwise, you end up with wwwroot\wwwroot folders.

使用本機工具與發佈Use local tools and publishing

您可以使用各種工具來製作及發佈函數應用程式,包括 Visual StudioVisual Studio CodeIntelliJEclipseAzure Functions Core ToolsFunction apps can be authored and published using a variety of tools, including Visual Studio, Visual Studio Code, IntelliJ, Eclipse, and the Azure Functions Core Tools. 如需詳細資訊,請參閱如何在本機撰寫 Azure Functions 程式碼並進行測試For more information, see Code and test Azure Functions locally.

如何在 Azure 入口網站中編輯函式How to edit functions in the Azure portal

Azure 入口網站內建的函式編輯器可讓您更新您直接內嵌的程式碼和 function.json 檔案。The Functions editor built into the Azure portal lets you update your code and your function.json file directly inline. 這僅建議用於概念的小型變更或證明 - 最佳作用是使用如 VS Code 等本機開發工具。This is recommended only for small changes or proofs of concept - best practice is to use a local development tool like VS Code.

平行執行Parallel execution

發生速度比單一執行緒函數執行階段快的多個觸發事件可以處理它們時,執行階段可能會平行多次叫用函數。When multiple triggering events occur faster than a single-threaded function runtime can process them, the runtime may invoke the function multiple times in parallel. 如果函式應用程式使用取用主控方案,則函式應用程式可以自動相應放大。If a function app is using the Consumption hosting plan, the function app could scale out automatically. 不論應用程式是在取用主控方案還是一般 App Service 主控方案上執行,函式應用程式的每個執行個體可能都會使用多個執行緒平行處理並行函式引動。Each instance of the function app, whether the app runs on the Consumption hosting plan or a regular App Service hosting plan, might process concurrent function invocations in parallel using multiple threads. 每個函式應用程式執行個體中的並行函式叫用次數上限,根據使用的觸發程序類型及函式應用程式內其他函式所使用的資源而有所不同。The maximum number of concurrent function invocations in each function app instance varies based on the type of trigger being used as well as the resources used by other functions within the function app.

Functions 執行階段版本設定Functions runtime versioning

您可以使用 FUNCTIONS_EXTENSION_VERSION 應用程式設定來設定 Functions 執行階段的版本。You can configure the version of the Functions runtime using the FUNCTIONS_EXTENSION_VERSION app setting. 例如,值 "~ 3" 表示您的函數應用程式將使用3.x 作為其主要版本。For example, the value "~3" indicates that your Function App will use 3.x as its major version. 函數應用程式會在發行時升級為每個新的次要版本。Function Apps are upgraded to each new minor version as they are released. 如需詳細資訊 (包括如何檢視函式應用程式的完全版本),請參閱如何設定 Azure Functions 的執行階段目標版本For more information, including how to view the exact version of your function app, see How to target Azure Functions runtime versions.

儲存機制Repositories

Azure Functions 的程式碼是開放原始碼,儲存於 GitHub 儲存機制中︰The code for Azure Functions is open source and stored in GitHub repositories:

繫結Bindings

以下是所有已支援繫結的表格。Here is a table of all supported bindings.

下表顯示 Azure Functions 執行時間的主要版本所支援的系結:This table shows the bindings that are supported in the major versions of the Azure Functions runtime:

TypeType 1.x1.x 2.x 和更新版本12.x and higher1 觸發程序Trigger 輸入Input 輸出Output
Blob 儲存體Blob storage
Cosmos DBCosmos DB
Event GridEvent Grid
事件中樞Event Hubs
HTTP & webhookHTTP & webhooks
IoT 中心IoT Hub
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 Starting with the version 2.x runtime, all bindings except HTTP and Timer must be registered. 請參閱註冊繫結延伸模組See Register binding extensions.

遇到繫結發生錯誤的問題嗎?Having issues with errors coming from the bindings? 請檢閱 Azure Functions 繫結錯誤碼文件。Review the Azure Functions Binding Error Codes documentation.

報告問題Reporting Issues

ItemItem 描述Description 連結Link
執行階段Runtime Script Host、觸發程序和繫結、語言支援Script Host, Triggers & Bindings, Language Support 提出問題File an Issue
範本Templates 建立範本的程式碼問題Code Issues with Creation Template 提出問題File an Issue
入口網站Portal 使用者介面或經驗問題User Interface or Experience Issue 提出問題File an Issue

後續步驟Next steps

如需詳細資訊,請參閱下列資源:For more information, see the following resources: