使用 Visual Studio 建立第一個函式Create your first function using Visual Studio

Azure Functions 可讓您在無伺服器環境中執行程式碼,而不需要先建立 VM 或發佈 Web 應用程式。Azure Functions lets you execute your code in a serverless environment without having to first create a VM or publish a web application.

在本文中,您將了解如何使用 Visual Studio 2019 在本機建立及測試 "hello world" 函式,然後將其發佈至 Azure。In this article, you learn how to use Visual Studio 2019 to locally create and test a "hello world" function and then publish it to Azure. 本快速入門針對 Visual Studio 2019 所設計。This quickstart is designed for Visual Studio 2019. 使用 Visual Studio 2017 建立 Functions 專案時,您必須先安裝最新的 Azure Functions 工具When creating a Functions project using Visual Studio 2017, you must first install the latest Azure Functions tools.

瀏覽器中的函式 localhost 回應

必要條件Prerequisites

若要完成本教學課程,您必須先安裝 Visual Studio 2019To complete this tutorial, you must first install Visual Studio 2019. 確定也已經安裝 Azure 開發工作負載。Make sure that the Azure development workload is also installed.

透過 Azure 開發工作負載安裝 Visual Studio

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

建立函式應用程式專案Create a function app project

Visual Studio 中的 Azure Functions 專案範本會建立可發行至 Azure 中函式應用程式的專案。The Azure Functions project template in Visual Studio creates a project that can be published to a function app in Azure. 您可以使用函式應用程式將多個函式群組為邏輯單位,以便您管理、部署和共用資源。You can use a function app to group functions as a logical unit for management, deployment, and sharing of resources.

  1. 在 Visual Studio 中,於 [檔案] 功能表上選取 [新增] > [專案] 。In Visual Studio, on the File menu, select New > Project.

  2. 在 [建立新專案] 對話方塊中,搜尋 functions,選擇 [Azure Functions] 範本,然後選取 [下一步] 。In the Create a new project dialog box, search for functions, choose the Azure Functions template, and select Next.

  3. 輸入專案的名稱,然後選取 [建立] 。Enter a name for your project, and select Create. 函式應用程式名稱必須是有效的 C# 命名空間,因此不會使用底線、連字號或任何其他非英數字元。The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.

  4. 在 [建立新的 Azure Functions 應用程式] 中,使用下列選項:In Create a new Azure Functions application, use the following options:

    • Azure Functions v2 (.NET Core) 1Azure Functions v2 (.NET Core) 1
    • HTTP 觸發程序HTTP trigger
    • 儲存體帳戶儲存體模擬器Storage Account: Storage Emulator
    • 授權層級 匿名Authorization level: Anonymous
    選項Option 建議的值Suggested value 說明Description
    Functions 執行階段Functions runtime Azure Functions 2.x
    (.NET Core)
    Azure Functions 2.x
    (.NET Core)
    此設定會建立使用 Azure Functions 2.x 版執行階段 (支援 .NET Core) 的函式專案。This setting creates a function project that uses the version 2.x runtime of Azure Functions, which supports .NET Core. Azure Functions 1.x 支援 .NET Framework。Azure Functions 1.x supports the .NET Framework. 如需詳細資訊,請參閱設定 Azure Functions 執行階段目標版本For more information, see Target Azure Functions runtime version.
    函式範本Function template HTTP 觸發程序HTTP trigger 此設定會建立 HTTP 要求所觸發的函式。This setting creates a function triggered by an HTTP request.
    儲存體帳戶Storage Account 儲存體模擬器Storage Emulator HTTP 觸發程序不會使用 Azure 儲存體帳戶連線。An HTTP trigger doesn't use the Azure Storage account connection. 其他所有觸發程序類型都需要有效的儲存體帳戶連接字串。All other trigger types require a valid Storage account connection string. 因為 Functions 需要儲存體帳戶,所以當您將專案發佈至 Azure 時,就會指派或建立一個。Because Functions requires a storage account, one is assigned or created when you publish your project to Azure.
    授權層級Authorization level 匿名Anonymous 建立的函式可以由任何用戶端觸發,而不需提供金鑰。The created function can be triggered by any client without providing a key. 此授權設定可以讓測試新函式變得簡單。This authorization setting makes it easy to test your new function. 如需金鑰和授權的詳細資訊,請參閱 HTTP 和 Webhook 繫結中的授權金鑰For more information about keys and authorization, see Authorization keys in the HTTP and webhook bindings.

    注意

    請確定您已將 [授權層級] 設定為 AnonymousMake sure you set the Authorization level to Anonymous. 如果您選擇預設層級 Function 時,必須在要求中提供函式金鑰以存取您的函式端點。If you choose the default level of Function, you're required to present the function key in requests to access your function endpoint.

  5. 選取 [建立] 以建立函式專案和由 HTTP 觸發的函式。Select Create to create the function project and HTTP-triggered function.

Visual Studio 會建立一個專案,其中的類別包含 HTTP 觸發程序函式類型的重複使用程式碼。Visual Studio creates a project and class that contains boilerplate code for the HTTP trigger function type. 方法上的 FunctionName 屬性會設定函式名稱,其預設為 HttpTriggerThe FunctionName attribute on the method sets the name of the function, which by default is HttpTrigger. HttpTrigger 屬性指定此函式是由 HTTP 要求所觸發。The HttpTrigger attribute specifies that the function is triggered by an HTTP request. 重複使用程式碼會傳送 HTTP 回應,其中包含要求內文或查詢字串中的值。The boilerplate code sends an HTTP response that includes a value from the request body or query string.

您可以將適當的屬性套用至方法,進而使用輸入和輸出繫結來擴展您的函式功能。You can expand the capabilities of your function using input and output bindings by applying the appropriate attributes to the method. 如需詳細資訊,請參閱 Azure Functions C# 開發人員參考觸發程序和繫結一節。For more information, see the Triggers and bindings section of the Azure Functions C# developer reference.

您現在已建立函式專案和 HTTP 觸發的函式,可以在本機電腦上進行測試。Now that you've created your function project and an HTTP-triggered function, you can test it on your local computer.

在本機執行函式Run the function locally

Visual Studio 與 Azure Functions Core Tools 整合,以便您使用完整 Functions 執行階段在本機測試函式。Visual Studio integrates with Azure Functions Core Tools so that you can test your functions locally using the full Functions runtime.

  1. 若要執行您的函式,請按 F5To run your function, press F5. 您可能需要啟用防火牆例外狀況,工具才能處理 HTTP 要求。You may need to enable a firewall exception so that the tools can handle HTTP requests. 在本機執行時,不會強制執行授權層級。Authorization levels are never enforced when running locally.

  2. 從 Azure Functions 執行階段輸出複製函式的 URL。Copy the URL of your function from the Azure Functions runtime output.

    Azure 本機執行階段

  3. 將 HTTP 要求的 URL 貼到瀏覽器的網址列。Paste the URL for the HTTP request into your browser's address bar. 將查詢字串 ?name=<YOUR_NAME> 附加至此 URL 並執行要求。Append the query string ?name=<YOUR_NAME> to this URL and execute the request. 下圖顯示瀏覽器中對於函式傳回之本機 GET 要求所做出的回應︰The following shows the response in the browser to the local GET request returned by the function:

    瀏覽器中的函式 localhost 回應

  4. 若要停止偵錯,請按 Shift + F5To stop debugging, press Shift + F5.

確認函式在本機電腦上正確執行之後,就可以將專案發佈到 Azure。After you have verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

將專案發佈到 AzurePublish the project to Azure

您的 Azure 訂用帳戶中必須具有函式應用程式,才可以發佈您的專案。You must have a function app in your Azure subscription before you can publish your project. Visual Studio 發佈會在您第一次發佈專案時,為您建立函式應用程式。Visual Studio publishing creates a function app for you the first time you publish your project.

  1. 在 [方案總管] 中,以滑鼠右鍵按一下專案並選取 [發佈] 。In Solution Explorer, right-click the project and select Publish.

  2. 在 [挑選發佈目標] 對話方塊中,使用影像下方表格中所指定的發佈選項:In the Pick a publish target dialog, use the publish options as specified in the table below the image:

    挑選發行目標

    選項Option 說明Description
    Azure Functions 取用方案Azure Functions Consumption plan 當您將專案發佈至在取用方案中執行的函式應用程式時,您只需支付您的函式應用程式執行費用。When you publish your project to a function app that runs in a Consumption plan, you only pay for executions of your functions app. 其他主控方案會產生較高的成本。Other hosting plans incur higher costs. 若要深入了解,請參閱 Azure Functions 規模調整和主控To learn more, see Azure Functions scale and hosting.
    建立新項目Create new 在 Azure 中會建立具有相關資源的新函式應用程式。A new function app, with related resources, is created in Azure. 當您選擇 [選取現有的] ,Azure 中現有函式應用程式的所有檔案都會以本機專案中的檔案加以覆寫。When you choose Select Existing, all files in the existing function app in Azure are overwritten by files from the local project. 請在將更新重新發佈至現有函式應用程式時,才使用此選項。Only use this option when republishing updates to an existing function app.
    從套件檔案執行Run from package file 您的函式應用程式會使用已啟用從套件執行模式的 Zip 部署來部署。Your function app is deployed using Zip Deploy with Run-From-Package mode enabled. 這是執行函式的建議方法,其可產生更好的效能。This is the recommended way of running your functions, which results in better performance. 若不使用此選項,請務必在發佈到 Azure 前,先停止在本機執行函式應用程式專案。When not using this option, make sure to stop your function app project from running locally before you publish to Azure.
  3. 選取 [發佈] 。Select Publish. 如果您尚未從 Visual Studio 登入您的 Azure 帳戶,請選取 [登入] 。If you haven't already signed-in to your Azure account from Visual Studio, select Sign-in. 您也可以建立免費 Azure 帳戶。You can also create a free Azure account.

  4. 在 [App Service: 新建] 對話方塊中,使用影像下方表格中指定的 [主控] 設定:In the App Service: Create new dialog, use the Hosting settings as specified in the table below the image:

    建立 App Service 對話方塊

    設定Setting 建議的值Suggested value 說明Description
    名稱Name 全域唯一的名稱Globally unique name 用以唯一識別新函式應用程式的名稱。Name that uniquely identifies your new function app. 有效字元是 a-z0-9-Valid characters are a-z, 0-9, and -.
    訂用帳戶Subscription 選擇您的訂用帳戶Choose your subscription 要使用的 Azure 訂用帳戶。The Azure subscription to use.
    資源群組Resource Group myResourceGroupmyResourceGroup 要在其中建立函式應用程式的資源群組名稱。Name of the resource group in which to create your function app. 選擇 [新增] 以建立新的資源群組。Choose New to create a new resource group.
    主控方案Hosting Plan 取用方案Consumption plan 在您選取 [新增] 以建立無伺服器方案之後,請務必選擇 [大小] 之下的 [取用] 。Make sure to choose the Consumption under Size after you select New to create a serverless plan. 或者,在區域中選擇您附近的 [位置] ,或選擇與函式將會存取之其他服務接近的位置。Also, choose a Location in a region near you or near other services your functions access. 取用以外的方案中執行時,您必須管理函式應用程式的調整When you run in a plan other than Consumption, you must manage the scaling of your function app.
    Azure 儲存體Azure Storage 一般用途的儲存體帳戶General-purpose storage account Functions 執行階段需要 Azure 儲存體帳戶。An Azure storage account is required by the Functions runtime. 選取 [新增] 以建立一般用途的儲存體帳戶。Select New to create a general-purpose storage account. 您也可以使用符合儲存體帳戶需求的現有帳戶。You can also use an existing account that meets the storage account requirements.
  5. 選取 [建立] 以在 Azure 中使用這些設定建立函式應用程式和相關資源,並且部署函式專案程式碼。Select Create to create a function app and related resources in Azure with these settings and deploy your function project code.

  6. 部署完成之後,請記下 [網站 URL] 值,這是 Azure 中您函式應用程式的位址。After the deployment is complete, make a note of the Site URL value, which is the address of your function app in Azure.

    發行成功訊息

在 Azure 中測試您的函式Test your function in Azure

  1. 從發行設定檔頁面複製函式應用程式的基底 URL。Copy the base URL of the function app from the Publish profile page. 使用新的基底 URL,取代在本機測試函式時所使用之 URL 的 localhost:port 部分。Replace the localhost:port portion of the URL you used when testing the function locally with the new base URL. 如同以往,務必將查詢字串 ?name=<YOUR_NAME> 附加至此 URL 並執行要求。As before, make sure to append the query string ?name=<YOUR_NAME> to this URL and execute the request.

    呼叫 HTTP URL 觸發函式的 URL 應採用下列格式:The URL that calls your HTTP triggered function should be in the following format:

     http://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?name=<YOUR_NAME> 
    
  2. 將 HTTP 要求的新 URL 貼到瀏覽器的網址列。Paste this new URL for the HTTP request into your browser's address bar. 下圖顯示瀏覽器中對於函式傳回之遠端 GET 要求所做出的回應︰The following shows the response in the browser to the remote GET request returned by the function:

    瀏覽器中的函式回應

後續步驟Next steps

您已透過 Visual Studio,使用簡單的 HTTP 觸發函式在 Azure 中建立及發佈 C# 函式應用程式。You have used Visual Studio to create and publish a C# function app in Azure with a simple HTTP triggered function. 若要深入了解如何將函式開發為 .NET 類別庫,請參閱 Azure Functions C# 開發人員參考To learn more about developing functions as .NET class libraries, see Azure Functions C# developer reference.