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

使用 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. 若要运行函数,请按 F5 。To 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. 在“应用服务: 新建”对话框中,使用图片下方的表中指定的“托管”设置: In the App Service: Create new dialog, use the Hosting settings as specified in the table below the image:

    “创建应用服务”对话框

    设置Setting 建议的值Suggested value 说明Description
    名称Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。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 要在其中创建 Function App 的资源组的名称。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 的 localhost:port 部分(在本地测试函数时使用)替换为新的基 URL。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 应采用以下格式: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 在 Azure 中创建和发布使用简单的 HTTP 触发函数的 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.