您现在访问的是微软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.

本文介绍如何使用 Azure Functions 的 Visual Studio 2017 工具在本地创建并测试“hello world”函数。In this article, you learn how to use the Visual Studio 2017 tools for Azure Functions to locally create and test a "hello world" function. 然后将函数代码发布到 Azure。You then publish the function code to Azure. Visual Studio 2017 中的 Azure 开发工作负荷已随附这些工具。These tools are available as part of the Azure development workload in Visual Studio 2017.

浏览器中的函数 localhost 响应

本主题包括一部视频,其中演示了相同的基本步骤。This topic includes a video that demonstrates the same basic steps.

先决条件Prerequisites

完成本教程:To complete this tutorial:

如果还没有 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. 在“新建项目”对话框中,依次选择“已安装” > “Visual C#” > “云” > “Azure Functions”。In the New Project dialog box, select Installed > Visual C# > Cloud > Azure Functions. 输入项目的名称,并选择“确定”。Enter a name for your project, and select OK. 函数应用名称必须可以充当 C# 命名空间,因此请勿使用下划线、连字符或任何其他的非字母数字字符。The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.

    “新建项目”对话框,用于在 Visual Studio 中创建函数

  3. 使用图像后的表中指定的设置。Use the settings specified in the table that follows the image.

    Visual Studio 中的“新建函数”对话框

    设置Setting 建议的值Suggested value DescriptionDescription
    版本Version Azure Functions 2.xAzure Functions 2.x
    (.NET Core)(.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.
    模板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.
    访问权限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 in the HTTP and webhook bindings.

    备注

    确保将“访问权限”设置为 AnonymousMake sure you set the Access rights 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.

  4. 选择“确定”以创建函数项目和 HTTP 触发的函数。Select OK to create the function project and HTTP-triggered function.

Visual Studio 将创建一个项目,并在该项目中创建一个包含所选函数类型的样本代码的类。Visual Studio creates a project and in it a class that contains boilerplate code for the chosen function type. 方法中的 FunctionName 属性设置函数的名称。The FunctionName attribute on the method sets the name of the function. 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 add input and output bindings to a function 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.

在本地测试函数Test the function locally

使用 Azure Functions Core Tools 可以在本地开发计算机上运行 Azure Functions 项目。Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. 首次从 Visual Studio 启动某个函数时,系统会提示你安装这些工具。You are prompted to install these tools the first time you start a function from Visual Studio.

  1. 若要测试函数,请按 F5。To test your function, press F5. 如果系统提示,请按 Visual Studio 的请求下载和安装 Azure Functions Core (CLI) 工具。If prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. 可能还需启用一个防火墙例外,以便这些工具能够处理 HTTP 请求。You may also need to enable a firewall exception so that the tools can handle HTTP requests.

  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 创建函数应用。You can create a function app right from Visual Studio.

  1. 在“解决方案资源管理器” 中,右键单击该项目并选择“发布”。In Solution Explorer, right-click the project and select Publish.

  2. 依次选择“Azure Function App”、“新建”、“发布”。Select Azure Function App, choose Create New, and then select Publish.

    选取发布目标

    启用“从 Zip 运行”后,Azure 中的函数应用将进入只读模式,并且会直接从部署包运行。When you enable Run from Zip, your function app in Azure goes into read-only mode and is run directly from the deployment package. 有关详细信息,请参阅从包文件运行 Azure FunctionsFor more information, see Run your Azure Functions from a package file.

    注意

    选择“选择现有”时,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.

  3. 如果尚未将 Visual Studio 连接到 Azure 帐户,请选择“添加帐户...”。If you haven't already connected Visual Studio to your Azure account, select Add an account....

  4. 在“创建应用服务”对话框中,使用在图片下方的表中指定的“托管”设置:In the Create App Service dialog, use the Hosting settings as specified in the table below the image:

    “创建应用服务”对话框

    设置Setting 建议的值Suggested value 说明Description
    应用名称App Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。Name that uniquely identifies your new function app.
    订阅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.
    应用服务计划App Service Plan 消耗量计划Consumption plan 单击“新建”创建某个无服务器计划后,确保在“大小”下选择“消耗量”。Make sure to choose the Consumption under Size after you click 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.
    存储帐户Storage Account 常规用途存储帐户General purpose storage account Functions 运行时需要 Azure 存储帐户。An Azure storage account is required by the Functions runtime. 单击“新建”创建一个常规用途存储帐户。Click New to create a general purpose storage account. 也可使用符合存储帐户要求的现有帐户。You can also use an existing account that meets the storage account requirements.
  5. 单击“创建”以使用这些设置在 Azure 中创建函数应用和相关资源,并部署函数项目代码。Click 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:

    浏览器中的函数响应

观看视频Watch the video

后续步骤Next steps

你已使用简单的 HTTP 触发函数通过 Visual Studio 创建和发布 C# 函数应用。You have used Visual Studio to create and publish a C# function app with a simple HTTP triggered function.