您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

在本文中,我们使用 Visual Studio 来创建一个响应 HTTP 请求的基于 C# 类库的函数。In this article, you use Visual Studio to create a C# class library-based function that responds to HTTP requests. 在本地测试代码后,将代码部署到 Azure Functions 的无服务器环境。After testing the code locally, you deploy it to the serverless environment of Azure Functions.

完成本快速入门会从你的 Azure 帐户中扣取最多几美分的费用。Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.

先决条件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 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 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 Functions application settings, use the values in the following table:

    设置Setting Value 说明Description
    Functions 运行时Functions runtime Azure Functions v3
    (.NET Core)
    Azure Functions v3
    (.NET Core)
    此值会创建使用 Azure Functions 的版本 3.x 运行时(支持 .NET Core 3.x)的函数项目。This value creates a function project that uses the version 3.x runtime of Azure Functions, which supports .NET Core 3.x. 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.
    存储帐户(AzureWebJobsStorage)Storage account (AzureWebJobsStorage) 存储模拟器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.
    授权级别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 and HTTP and webhook bindings.

    Azure Functions 项目设置

    请确保将“授权级别”设置为“匿名”。 Make sure you set the Authorization level 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. 样本代码发送 HTTP 响应,其中包含来自请求正文或查询字符串的值。The boilerplate code sends an HTTP response that includes a value from the request body or query string. HttpTrigger 属性指定该函数将由某个 HTTP 请求触发。The HttpTrigger attribute specifies that the function is triggered by an HTTP request.

重命名函数Rename the function

FunctionName 方法属性设置函数的名称(默认情况下生成为 Function1)。The FunctionName method attribute sets the name of the function, which by default is generated as Function1. 由于工具不允许在创建项目时覆盖默认函数名称,请花一点时间为函数类、文件和元数据创建更好的名称。Since the tooling doesn't let you override the default function name when you create your project, take a minute to create a better name for the function class, file, and metadata.

  1. 在“文件资源管理器”中,右键单击 Function1.cs 文件并将其重命名为 HttpExample.csIn File Explorer, right-click the Function1.cs file and rename it to HttpExample.cs.

  2. 在代码中,将 Function1 类重命名为 HttpExampleIn the code, rename the Function1 class to `HttpExample'.

  3. 在名为 RunHttpTrigger 方法中,将 FunctionName 方法属性重命名为 HttpExampleIn the HttpTrigger method named Run, rename the FunctionName method attribute to HttpExample.

重命名函数以后,即可在本地计算机上对其进行测试。Now that you've renamed the 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 free 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 在靠近你或者靠近函数访问的其他服务的区域中选择一个位置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. 选择“发布”,然后等待部署完成。Select Publish and wait for the deployment to complete.

    部署完成后,Azure 中函数应用的根 URL 将显示在“发布”选项卡中。After the deployment completes the root URL of the function app in Azure is shown in the Publish tab.

  7. 在“发布”选项卡中,请选择“在 Cloud Explorer 中管理”。In the Publish tab, choose Manage in Cloud Explorer. 此操作会在 Cloud Explorer 中打开新的函数应用 Azure 资源。This opens the new function app Azure resource in Cloud Explorer.

    创建发布配置文件

    借助 Cloud Explorer,可使用 Visual Studio 来查看网站的内容,启动和停止函数应用,并直接浏览到 Azure 上和 Azure 门户中的函数应用资源。Cloud Explorer lets you use Visual Studio to view the contents of the site, start and stop the function app, and browse directly to function app resources on Azure and in the Azure portal.

在 Azure 中测试函数Test your function in Azure

  1. 在 Cloud Explorer 中,应已选中新的函数应用。In Cloud Explorer, your new function app should be selected. 如果未选中,请依次展开你的订阅>“应用服务”,并选择新的函数应用。If not, expand your subscription > App Services, and select your new function app.

  2. 右键单击函数应用,然后选择“在浏览器中打开”。Right-click the function app and choose Open in Browser. 这会在默认的 Web 浏览器中打开函数应用的根,并显示指示函数应用正在运行的页面。This opens the root of your function app in your default web browser and displays the page that indicates your function app is running.

    函数应用正在运行

  3. 在浏览器的地址栏中,将字符串 /api/HttpExample?name=Functions 追加到基 URL,然后运行请求。In the address bar in the browser, append the string /api/HttpExample?name=Functions to the base URL and run the request.

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

    http://<APP_NAME>.azurewebsites.net/api/HttpExample?name=Functions

  4. 转到此 URL,你会在浏览器中看到函数返回的对远程 GET 请求的响应,如下例所示:Go to this URL and you see a response in the browser to the remote GET request returned by the function, which looks like the following example:

    函数应用正在运行

清理资源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. 在 Cloud Explorer 中,请依次展开你的订阅>“应用服务”,右键单击函数应用,然后选择“在门户中打开” 。In the Cloud Explorer, expand your subscription > App Services, right-click your function app, and choose Open in Portal.

  2. 在函数应用页中,请选择“概览”选项卡,然后选择“资源组”下的链接 。In the function app page, select the Overview tab and then select the link under Resource group.

    函数应用正在运行

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

  4. 选择“删除资源组”,然后按说明操作。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: