快速入门:使用 Visual Studio 在 Azure 中创建第一个函数Quickstart: Create your first function in Azure using Visual Studio

使用 Azure Functions,可以在无服务器环境中运行代码,而无需先创建 VM 或发布 Web 应用程序。Azure Functions lets you run your code in a serverless environment without having to first create a VM or publish a web application.

本快速入门介绍如何使用 Visual Studio 2019 在本地创建并测试“hello world” HTTP 触发器 C# 函数应用,然后将其发布到 Azure。In this quickstart, you learn how to use Visual Studio 2019 to locally create and test a "hello world" HTTP trigger C# function app, which you then publish to Azure.

浏览器中的函数 localhost 响应

本快速入门专为 Visual Studio 2019 设计。This quickstart is designed for Visual Studio 2019.

先决条件Prerequisites

若要完成本教程,请先安装 Visual Studio 2019To complete this tutorial, first install Visual Studio 2019. 请确保在安装过程中选择“Azure 开发”**** 工作负载。Ensure you select the Azure development workload during installation. 若要改为使用 Visual Studio 2017 创建 Azure Functions 项目,则必须先安装最新的 Azure Functions 工具If you want to create an Azure Functions project by using Visual Studio 2017 instead, you must first install the latest Azure Functions tools.

安装包含“Azure 开发”工作负载的 Visual Studio

如果没有 Azure 订阅,请在开始之前创建试用版If you don't have an Azure subscription, create a Trial 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 you can publish to a function app in Azure. 可使用函数应用将函数分组为逻辑单元,以便更轻松地管理、部署、缩放和共享资源。You can use a function app to group functions as a logical unit for easier management, deployment, scaling, and sharing of resources.

  1. 在 Visual Studio 菜单中,选择“文件” > “新建” > “项目”。From the Visual Studio menu, select File > New > Project.

  2. 在“创建新项目”中,在搜索框中输入“functions”,选择“Azure Functions”模板,然后选择“下一步”。In Create a new project, enter functions in the search box, choose the Azure Functions template, and then select Next.

  3. 在“配置新项目”中,输入项目的“项目名称”,然后选择“创建”。In Configure your new project, enter a Project name for your project, and then 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 应用程序”设置,请使用下表中的值:For the Create a new Azure Function Application settings, use the values in the following table:

    设置Setting Value 说明Description
    Functions 运行时Functions runtime Azure Functions v2
    (.NET Core)
    Azure Functions v2
    (.NET Core)
    此值会创建使用 Azure Functions 的版本 2.x 运行时(支持 .NET Core)的函数项目。This value 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 Azure Functions runtime versions overview.
    函数模板Function template HTTP 触发器HTTP trigger 此值会创建由 HTTP 请求触发的函数。This value creates a function triggered by an HTTP request.
    存储帐户Storage Account 存储模拟器Storage Emulator 由于 Azure 函数需要存储帐户,因此在将项目发布到 Azure 时会分配或创建一个存储帐户。Because an Azure Function requires a storage account, one is assigned or created when you publish your project to Azure. HTTP 触发器不使用 Azure 存储帐户连接字符串;所有其他触发器类型需要有效的 Azure 存储帐户连接字符串。An HTTP trigger doesn't use an Azure Storage account connection string; all other trigger types require a valid Azure Storage account connection string.
    访问权限Access rights 匿名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 and HTTP and webhook bindings.

    Azure Functions 项目设置

    确保将“访问权限”设置为“匿名”。Make sure you set the Access rights to Anonymous. 如果选择默认级别的函数,需要在请求中提供函数密钥才能访问函数终结点。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 trigger function.

Visual Studio 将创建一个项目和一个包含 HTTP 触发器函数类型样本代码的类。Visual Studio creates a project and class that contains boilerplate code for the HTTP trigger function type. FunctionName 方法属性设置函数的名称(默认情况下为 Function1)。The FunctionName method attribute sets the name of the function, which by default is Function1. 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.

使用输入和输出绑定来扩展函数的功能,具体的做法是将适当的属性应用于方法。Expand the capabilities of your function with 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 trigger function, you can test it on your local computer.

在本地运行函数Run the function locally

Visual Studio 与 Azure Functions Core Tools 集成,这样你便可使用完整的 Azure Functions 运行时在本地测试函数。Visual Studio integrates with Azure Functions Core Tools so that you can test your functions locally using the full Azure Functions runtime.

  1. 若要运行函数,请在 Visual Studio 中按 F5。To run your function, press F5 in Visual Studio. 你可能需要启用防火墙例外,这样工具才能处理 HTTP 请求。You might need to enable a firewall exception so that the tools can handle HTTP requests. 在本地运行函数时,永远不会强制实施授权级别。Authorization levels are never enforced when you run a function 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 run the request. 下图显示了浏览器中函数返回的对本地 GET 请求的响应。The following image shows the response in the browser to the local GET request returned by the function:

    浏览器中的函数 localhost 响应

  4. 若要停止调试,请在 Visual Studio 中按 Shift+F5。To stop debugging, press Shift+F5 in Visual Studio.

确认该函数可以在本地计算机上正确运行以后,即可将项目发布到 Azure。After you've 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 订阅中有一个函数应用,然后才能发布项目。Before you can publish your project, you must have a function app in your Azure subscription. 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 Pick a publish target, use the publish options specified in the following table:

    选项Option 说明Description
    Azure Functions 消耗计划Azure Functions Consumption Plan 在 Azure 云环境中创建一个在消耗计划中运行的函数应用。Create a function app in an Azure cloud environment that runs in a Consumption plan. 使用消耗计划时,只需为函数应用的执行付费。When you use a Consumption plan, you pay only for executions of your functions app. 其他托管计划会产生更高的成本。Other hosting plans incur higher costs. 如果在消耗计划之外的计划中运行,则必须管理函数应用的缩放If you run in a plan other than a Consumption plan, you must manage the scaling of your function app.
    新建Create New 在 Azure 中创建具有相关资源的新函数应用。A new function app, with related resources, is created in Azure.
    如果选择“选择现有”****,则 Azure 中现有函数应用中的所有文件都会被本地项目中的文件覆盖。If you choose Select Existing, all files in the existing function app in Azure are overwritten by files from the local project. 只有在将更新重新发布到现有函数应用时,才使用此选项。Use this option only when you republish 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 deployment, which results in better performance, is the recommended way of running your functions.
    如果不使用此选项,请确保在将函数应用项目发布到 Azure 之前,先停止其在本地的运行。If you don't use this option, make sure to stop your function app project from running locally before you publish to Azure.

    选取发布目标

  3. 选择“创建配置文件”****。Select Create Profile. 如果尚未从 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 Azure account.

  4. 在“应用服务:**** 新建”中,使用下表中指定的值:In App Service: Create new, use the values specified in the following table:

    设置Setting ValueValue 说明Description
    名称Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。Name that uniquely identifies your new function app. 接受此名称或输入新名称。Accept this name or enter a new name. 有效的字符是 a-z0-9-Valid characters are: a-z, 0-9, and -.
    订阅Subscription 你的订阅Your subscription 要使用的 Azure 订阅。The Azure subscription to use. 接受此订阅,或从下拉列表中选择一个新订阅。Accept this subscription or select a new one from the drop-down list.
    资源组Resource group 资源组的名称Name of your resource group 要在其中创建函数应用的资源组。The resource group in which to create your function app. 从下拉列表中选择现有资源组,或者选择“新建”来创建新的资源组。****Select an existing resource group from the drop-down list or choose New to create a new resource group.
    托管计划Hosting Plan 托管计划的名称Name of your hosting plan 选择“新建”即可配置一个无服务器计划。****Select New to configure a serverless plan. 确保在“大小”下选择“消耗量”。**** ****Make sure to choose the Consumption under Size. 将项目发布到在消耗计划中运行的函数应用时,只需为函数应用的执行付费。When you publish your project to a function app that runs in a Consumption plan, you pay only for executions of your functions app. 其他托管计划会产生更高的成本。Other hosting plans incur higher costs. 如果在“消耗”之外的计划中运行,则必须管理函数应用的缩放。****If you run in a plan other than Consumption, you must manage the scaling of your function app.
    位置Location 应用服务的位置Location of the app service 在靠近你或者靠近函数访问的其他服务的 region 中选择一个位置****。Choose a Location in a region near you or other services your functions access.
    Azure 存储Azure Storage 常规用途存储帐户General-purpose storage account Functions 运行时需要 Azure 存储帐户。An Azure Storage account is required by the Functions runtime. 选择“新建”即可配置常规用途存储帐户。****Select New to configure a general-purpose storage account. 也可选择一个符合存储帐户要求的现有帐户。You can also choose an existing account that meets the storage account requirements.

    “创建应用服务”对话框

  5. 选择“创建”****,使用这些设置在 Azure 中创建函数应用及其相关的资源,并部署函数项目代码。Select Create to create a function app and its related resources in Azure with these settings and deploy your function project code.

  6. 选择“发布”,并且在完成部署后记下“站点 URL”**** 值,这是函数应用在 Azure 中的地址。Select Publish and after the deployment completes, 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 的 localhost:port 部分(用于在本地测试函数)替换为新的基 URL。Replace the localhost:port portion of the URL you used to test the function locally with the new base URL. 将查询字符串 ?name=<YOUR_NAME> 追加到此 URL 并运行请求。Append the query string ?name=<YOUR_NAME> to this URL and run the request.

    调用 HTTP 触发器函数的 URL 采用以下格式:The URL that calls your HTTP trigger function is in the following format:

    http://<APP_NAME>.chinacloudsites.cn/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 image shows the response in the browser to the remote GET request returned by the function:

    浏览器中的函数响应

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算使用后续的快速入门、教程或者在本快速入门中创建的任何服务,请勿清理这些资源。If you plan to work with subsequent quickstarts, tutorials, or with any of the services you have created in this quickstart, do not clean up the resources.

Azure 中的资源是指函数应用、函数、存储帐户等。Resources in Azure refer to function apps, functions, storage accounts, and so forth. 这些资源可以组合到资源组 中,删除该组即可删除组中的所有内容。They're grouped into resource groups, and you can delete everything in a group by deleting the group.

已创建完成这些快速入门所需的资源。You created resources to complete these quickstarts. 这些资源可能需要付费,具体取决于帐户状态服务定价You may be billed for these resources, depending on your account status and service pricing. 如果不再需要这些资源,请参阅下面介绍的资源删除方法:If you don't need the resources anymore, here's how to delete them:

  1. 在 Azure 门户中转到“资源组”页。 In the Azure portal, go to the Resource group page.

    若要从函数应用页转到该页,请选择“概览”选项卡,然后选择“资源组”下的链接。 To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    从函数应用页选择要删除的资源组。

    若要从仪表板转到该页,请选择“资源组”,然后选择用于本快速入门的资源组。 To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. 在“资源组”页中查看所包括的资源的列表,然后验证这些资源是否是要删除的。 In the Resource group page, review the list of included resources, and verify that they're the ones you want to delete.

  3. 选择“删除资源组” ,然后按说明操作。Select Delete resource group, and follow the instructions.

    可能需要数分钟才能删除完毕。Deletion may take a couple of minutes. 完成后会显示一个通知,持续数秒。When it's done, a notification appears for a few seconds. 也可以选择页面顶部的钟形图标来查看通知。You can also select the bell icon at the top of the page to view the notification.

后续步骤Next steps

在本快速入门中,你已使用 Visual Studio,在 Azure 中创建并发布了一个 C# 函数应用,其中包含一个简单的 HTTP 触发器函数。In this quickstart, you used Visual Studio to create and publish a C# function app in Azure with a simple HTTP trigger function.

请转到下一篇文章,了解如何将一个 Azure 存储队列绑定添加到函数:Advance to the next article to learn how to add an Azure Storage queue binding to your function: