Azure Functions 簡介Introduction to Azure Functions

Azure Functions 是在雲端建立和執行事件驅動程式碼片段(函式–-)的方式,而不需要明確布建或管理基礎結構。Azure Functions is a way to create and run event-driven snippets of code –– functions –– in the cloud, without having to explicitly provision or manage infrastructure. 如需 Azure Functions 的詳細資訊,請參閱 Azure Functions 文件For more information about Azure Functions, see the Azure Functions documentation.

需求Requirements

Azure 函式工具隨附於 Visual Studio for Mac 7.5 和更新版本。Azure Function tools are included in Visual Studio for Mac 7.5 and newer.

若要建立和部署函式,您也需要 Azure 訂用帳戶。To create and deploy functions you also need an Azure subscription. 如果您沒有 Azure 帳戶,您可以立即免費註冊,並獲得12個月免費的熱門服務、$200 的免費點數和25項以上永遠免費的服務-> https://azure.com/freeIf you don't have an Azure account, you can sign up today for free and receive 12 months of free popular services, $200 free credit and 25+ always free services -> https://azure.com/free.

建立您的第一個 Azure Functions 專案Creating your first Azure Functions project

  1. 在 Visual Studio for Mac 中,選取 [檔案] > [新方案 ]。In Visual Studio for Mac, select File > New Solution.

  2. 從 [新增專案] 對話方塊,選取 [雲端] > [一般] 下的 Azure Functions 範本,然後按一下 [下一步]:From the New Project dialog, select the Azure Functions template under Cloud > General and click Next :

    顯示 Azure Functions 選項的 [新增專案] 對話方塊

  3. 選取您想要使用的初始 Azure Functions 範本,並輸入您的函數名稱,然後按 [下一步]。Select the initial Azure Functions template that you wish to use, enter your function name and click Next.

    顯示 Azure Functions 範本的 [新增專案] 對話方塊

    提示

    即使配套的 Azure Functions 執行階段和範本 (CLI) 盡可能保持在最新,也無可避免會過時。While the bundled Azure Functions runtime and templates (CLI) are kept as to date as possible, they inevitably get outdated. 建立新的 Functions 專案時,Visual Studio for Mac 會檢查是否有 CLI 更新並通知您,如下圖所示。When creating a new Functions project, Visual Studio for Mac will check for updates to the CLI and will notify you as shown in the image below. 只要按一下按鈕即可下載更新的範本。Simply click on the button to download the updated templates. 顯示有 Azure Functions 更新的 [新增專案] 對話方塊New project dialog showing Azure Functions updates are available

    根據您選取的函數類型,下一頁將會提示您鍵入詳細資料 (例如存取權限),如下圖所示:Depending on the type of function you select, the next page will prompt you to enter details, such as access rights, as illustrated in the following image:

    顯示其他選項的 [新增專案] 對話方塊

    如需不同類型之 Azure Functions 範本以及設定每個範本所需之繫結屬性的詳細資訊,請參閱可用函數範本一節。For more information on the different types of Azure Functions templates and the binding properties required to configure each template, see the Available function templates section. 在此範例中,我們將使用存取權限設為匿名的 Http 觸發程序。For this example, we're using a Http trigger with access rights set to anonymous.

  4. 在您設定參數之後,請選擇專案的位置,然後按一下 [建立]。Once you've set the parameters, choose the location for the project and click Create.

Visual Studio for Mac 會建立包含預設函數的 .NET Standard 專案。Visual Studio for Mac creates a .NET Standard project with a default function included. 其中也包含多種 AzureWebJobs 套件以及 Newtonsoft.Json 套件的 NuGet 參考。It also includes NuGet references to a variety of AzureWebJobs packages, as well as the Newtonsoft.Json package.

Visual Studio for Mac 編輯器顯示來自範本的全新 Azure 函式

新專案包含下列檔案:The new project contains the following files:

  • your-function-name.cs – 此類別包含您所選取函數的未定案程式碼。your-function-name.cs – This class contains boilerplate code for the function that you selected. 其中包含具有函數名稱的 FunctionName 屬性,以及指定觸發函數項目的觸發程序屬性 (例如It contains a FunctionName attribute with the function name, and a trigger attribute that specifies what triggers the function (eg. HTTP 要求)。a HTTP request). 如需函式方法的詳細資訊,請參閱 Azure Functions C# 開發人員參考文章。For more information on the function method, refer to the Azure Functions C# developer reference article.
  • host.json - 此檔案描述 Functions 主機的全域設定選項。host.json – This file describes the global configuration options for Functions host. 如需範例檔案以及此檔案的可用設定相關資訊,請參閱 Azure Functions 的 host.json 參考For an example file and information on the available settings for this file, see the host.json reference for Azure Functions.
  • local.settings.json - 此檔案包含用於在本機執行函式的所有設定。local.settings.json – This file contains all the settings for running functions locally. Azure Functions Core Tools 會使用這些設定。These settings are used by the Azure Functions Core Tools. 如需詳細資訊,請參閱<Azure Functions Core Tools>一文中的本機設定檔For more information, see Local settings file in the Azure Functions Core Tools article.

現在您已在 Visual Studio for Mac 建立了新的 Azure Functions 專案,因此可從本機電腦試用預設 HTTP 觸發函式。Now that you've created a new Azure Functions project in Visual Studio for Mac, you can test out the default HTTP-triggered function from your local machine.

在本機測試函式Testing the function locally

透過 Visual Studio for Mac 中的 Azure Functions 支援,您可以在本機開發電腦上測試函式並對其偵錯。With Azure Functions support in Visual Studio for Mac you can test and debug your function on your local development computer.

  1. 若要在本機測試您的函式,請按 Visual Studio for Mac 中的 執行 按鈕:To test your function locally, press the Run button in Visual Studio for Mac:

    Visual Studio for Mac 中的開始偵錯按鈕

  2. 執行專案即開始對 Azure 函式進行本機偵錯,並會開啟新的終端機視窗,如下圖所示:Running the project starts local debugging on the Azure Function and opens a new Terminal window, as illustrated in the following image:

    顯示函式輸出的終端機視窗

    請從輸出中複製 URL。Copy the URL from the output.

  3. 將 HTTP 要求的 URL 貼到瀏覽器的網址列。Paste the URL for the HTTP request into your browser's address bar. 在 URL 結尾新增查詢字串 ?name=<yourname>,然後執行要求。Add the query string ?name=<yourname> to the end of the URL and execute the request. 下圖顯示函式傳回瀏覽器中對本機 GET 要求的回應:The following image shows the response in the browser to the local GET request returned by the function:

    瀏覽器中的 http 要求

將另一個函數新增至專案Adding another function to your project

函式範本讓您能夠使用最常見的觸發程序和範本快速建立新函式。Function Templates enable you to quickly create new functions using the most common triggers and templates. 若要建立其他類型的函式,請執行下方作業:To create another type of function, do the following:

  1. 若要新增函式,請在專案名稱按一下滑鼠右鍵,然後選取 [新增] > [新增函式...]:To add a new function, right-click on the project name and select Add > Add Function... :

    新增函式的內容動作

  2. 從 [新增 Azure 函式] 對話方塊選取您要的函式:From the New Azure Function dialog, select the function you require:

    新增 Azure 函式對話方塊

    可用函數範本一節提供 Azure 函數範本的清單。A list of the Azure Function templates are provided in the Available function templates section.

您可以使用上述程序,對函式應用程式專案新增更多函式。You can use the above procedure to add more functions to your function app project. 專案中的每個函式都可以有不同的觸發程序,但函式必須只有一個觸發程序。Each function in the project can have a different trigger, but a function must have exactly one trigger. 如需詳細資訊,請參閱 Azure Functions 觸發程式和系結概念。For more information, see Azure Functions triggers and bindings concepts.

發佈至 AzurePublish to Azure

  1. 以滑鼠右鍵按一下專案名稱,然後選取 [發行] > 發佈到 azure 有發佈 > 發佈至 azure 的操作功能表 .。。選項已反白顯示Right-click on the project name and select Publish > Publish to Azure : Context menu with Publish > Publish to Azure... option highlighted

  2. 如果您已將 Azure 帳戶連線至 Visual Studio for Mac,則會顯示可用的應用程式服務清單。If you've already connected your Azure account to Visual Studio for Mac a list of available app services are displayed. 如果您尚未登入,則系統會提示您這麼做。If you haven't logged in, you'll be prompted to do so.

  3. 從 [Publish to Azure App Service] (發行至 Azure App Service) 對話方塊中,您可以選取現有應用程式服務,或按一下 [新增] 來建立新的應用程式服務。From the Publish to Azure App Service dialog, you can either select an existing app service or create a new one by clicking New.

  4. 在 [ 建立新的 App Service ] 對話方塊中,輸入您的設定: ![ 新增 App Service] 對話方塊,其中包含服務名稱、訂用帳戶、資源群組和服務方案設定的欄位。In the Create new App Service dialog, enter your settings: New App Service dialog, with fields for service name, subscription, resource group, and service plan settings.

    設定Setting 描述Description
    App Service 名稱App Service Name 識別新函數應用程式的全域唯一名稱。A globally unique name that identifies your new function app.
    訂用帳戶Subscription 要使用的 Azure 訂用帳戶。The Azure subscription to use.
    資源群組Resource Group 要在其中建立函式應用程式的資源群組名稱。Name of the resource group in which to create your function app. 選擇 + 建立新的資源群組。Choose + to create a new resource group.
    服務方案Service Plan 選擇現有方案,或建立自訂方案。Choose an existing plan or create a custom plan. 選擇區域中接近您或接近您函數所存取之其他服務的位置。Choose a Location in a region near you or near other services your functions access.
  5. 按 [下一步] 來建立儲存體帳戶。Click Next to create a storage account. Functions 執行階段需要 Azure 儲存體帳戶。An Azure storage account is required by the Functions runtime. 按一下 [自訂] 來建立一般用途儲存體帳戶,或使用現有儲存體帳戶:Click Custom to create a general purpose storage account, or use an existing one:

    新的 App Service 對話方塊,並提示您輸入儲存體帳戶名稱。

  6. 按一下 [建立] 以在 Azure 中使用這些設定建立函式應用程式和相關資源,並且部署函式專案程式碼。Click Create to create a function app and related resources in Azure with these settings and deploy your function project code.

  7. 系統可能會在發行期間使用對話方塊提示您,通知您「更新 Azure 上的函數版本」。You may be prompted with a dialog during publishing informing you to "Update Functions Version on Azure". 按一下 [是]Click Yes :

    提示要求「更新 Azure 應用程式設定以符合區域功能版本嗎?」

函數應用程式設定Function app settings

您在 local.settings.json 中所新增的所有設定,也必須新增至 Azure 中的函式應用程式。Any settings you added in the local.settings.json must be also added to the function app in Azure. 當您發行專案時,不會自動上傳這些設定。These settings are not uploaded automatically when you publish the project.

若要存取您的應用程式設定,請移至 Azure 入口網站 https://ms.portal.azure.com/To access your app settings, go to the Azure portal at https://ms.portal.azure.com/. 在 [函數應用程式] 下,選取 [函數應用程式],並反白顯示您的函數名稱:Under Functions Apps , select Function Apps and highlight your function name:

Azure 函數功能表

從 [概觀] 索引標籤中,選取 [已設定的功能] 下的 [應用程式設定]:From the Overview tab select Application settings under Configured features :

透過 Azure 函數的索引標籤

從這裡,您可以設定函數應用程式的應用程式設定,您可以在其中新增應用程式設定或修改現有應用程式設定:From here you can set Application Settings for the function app, where you can add new application settings or modify existing ones:

Azure 入口網站的應用程式設定區域

您可能需要設定的一個重要設定為 FUNCTIONS_EXTENSION_VERSIONOne important setting you may need to set is FUNCTIONS_EXTENSION_VERSION. 從 Visual Studio for Mac 發行時,此值應該設定為 betaWhen publishing from Visual Studio for Mac, this value should be set to beta.

可用的函式範本Available function templates

  • GitHub 觸發程序 - 回應您的 GitHub 存放庫中發生的事件。GitHub Trigger – Respond to events that occur in your GitHub repositories. 如需詳細資訊,請參閱有關 GitHub 的 Azure Functions 文章For more information, see the Azure Functions article on GitHub

    • GitHub 註解工具 - 這個函式會在收到問題或提取要求的 GitHub Webhook 時執行,並新增註解。GitHub commenter – This function will be run when it receives a GitHub webhook for an issue or pull request and adds a comment.
    • GitHub WebHook - 此函數會在收到 GitHub Webhook 時執行。GitHub WebHook – This function will be run when it receives a GitHub webhook.
  • HTTP - 使用 HTTP 要求來觸發程式碼執行。HTTP – Trigger the execution of your code by using an HTTP request. 下列 HTTP 觸發程序有明確的範本:There are explicit templates for the following HTTP triggers:

    • Http 觸發程序Http Trigger
    • Http GET CRUDHttp GET CRUD
    • Http POST CRUDHttp POST CRUD
    • 含有參數的 Http 觸發程序Http Trigger with parameters
  • 計時器 - 依預先定義的排程執行清理或其他批次工作。Timer – Execute cleanup or other batch tasks on a predefined schedule. 這個範本採用兩個欄位:名稱和排程,即六個欄位的 CRON 運算式。This template takes two fields: a Name and a schedule, which is a six field CRON expression. 如需詳細資訊,請參閱 有關時間的 Azure Functions 文章For more information, see the Azure Functions article on Time

  • 佇列觸發程序 - 這是會在訊息抵達 Azure 儲存體佇列時予以回應的函式。Queue Trigger – This is a function that will respond to messages as they arrive in the Azure Storage queue. 除了函式名稱之外,這個範本也接受 路徑 (會從中讀取訊息的佇列名稱) 和儲存體帳戶 連線 (包含儲存體帳戶連接字串的應用程式設定名稱)。In addition to the function name, this template takes a Path (the name of the queue from which the message will be read) and storage account Connection (the name of the app setting containing your storage account connection string). 如需詳細資訊,請參閱 佇列儲存體上的 Azure Functions 文章For more information, see the Azure Functions article on Queue Storage.

  • Blob 觸發程序 - 在 Azure 儲存體 Blob 新增到容器時加以處理。Blob Trigger – Process Azure Storage blobs when they are added to a container. 除了函式名稱之外,這個範本也接受路徑和連線屬性。In addition to the function name, this template also takes a path and connection property. 路徑屬性是觸發程序會監視的儲存體帳戶內部路徑。The path property is the path within your storage account that the trigger will monitor. 連線帳戶是包含儲存體帳戶連接字串的應用程式設定名稱。The connection account is the name of the app setting containing your storage account connection string. 如需詳細資訊,請參閱 Azure Functions Blob 儲存體文章For more information, see the Azure Functions Blob Storage article.

  • 泛型 WebHook - 這個簡單函式會在每次從支援 Webhook 的任何服務收到要求時執行。Generic WebHook – This is a simple function that will run whenever it receives a request from any service that supports webhooks. 如需詳細資訊,請參閱 有關泛型 webhook 的 Azure Functions 文章For more information, see the Azure Functions article on generic webhooks.

  • Durable Functions 協調流程 - Durable Functions 可讓您在無伺服器環境中撰寫具狀態函式。Durable functions orchestration – Durable Functions let you write stateful functions in a serverless environment. 此擴充功能會為您管理狀態、設定檢查點和重新啟動。The extension manages state, checkpoints, and restarts for you. 如需詳細資訊,請參閱長期 函數的 Azure Functions 指南。For more information, see the Azure Functions guides on Durable functions.

  • 影像大小調整工具 - 每當有 Blob 新增到容器時,這個函式都會建立調整過大小的影像。Image Resizer – This function creates resized images whenever a blob is added to a container. 這個範本接受路徑和連接字串、小型影像輸出及中型影像輸出。The template takes path and connection string for the trigger, a small image output, and a medium image output.

  • SAS 權杖 - 這個函式會為指定的 Azure 儲存體容器和 Blob 名稱產生 SAS 權杖。SAS token – This function generates a SAS token for a given Azure Storage container and blob name. 除了函式名稱之外,這個範本也接受路徑和連線屬性。In addition to the function name, this template also takes a path and connection property. 路徑屬性是觸發程序會監視的儲存體帳戶內部路徑。The path property is the path within your storage account that the trigger will monitor. 連線帳戶是包含儲存體帳戶連接字串的應用程式設定名稱。The connection account is the name of the app setting containing your storage account connection string. 存取權限 也需要設定。The Access rights also need to be set. 授權層級可控制函數是否需要 API 金鑰和要使用的金鑰;函數會使用函式金鑰;系統管理員會使用您的帳戶存取金鑰。Authorization level controls whether the function requires an API key and which key to use; Function uses a function key; Admin uses your account access key. 如需詳細資訊,請參閱 C# Azure Function for generating SAS tokens (用於產生 SAS 權杖的 C# Azure 函式) 樣本。For more information, see the C# Azure Function for generating SAS tokens sample.