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

使用 Visual Studio Code 创建你的第一个函数Create your first function using Visual Studio Code

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 Code 的 Azure Functions 扩展来通过 Microsoft Visual Studio Code 在本地计算机上创建和测试“hello world”函数。In this article, you learn how to use the Azure Functions extension for Visual Studio Code to create and test a "hello world" function on your local computer using Microsoft Visual Studio Code. 然后说明了如何将函数代码从 Visual Studio Code 发布到 Azure。You then publish the function code to Azure from Visual Studio Code.

Visual Studio 项目中的 Azure Functions 代码

该扩展目前完全支持 C#、JavaScript 和 Java 函数,Python 支持目前处于预览状态。The extension currently fully supports C#, JavaScript, and Java functions, with Python support currently in Preview. 本文中的步骤可能会因你为 Azure Functions 项目所选的语言而异。The steps in this article may vary depending on your choice of language for your Azure Functions project. 此扩展目前为预览版。The extension is currently in preview. 若要了解详细信息,请参阅适用于 Visual Studio Code 的 Azure Functions 扩展页。To learn more, see the Azure Functions extension for Visual Studio Code extension page.

先决条件Prerequisites

完成本快速入门教程需要:To complete this quickstart:

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

安装 Azure Functions 扩展Install the Azure Function extension

Azure Functions 扩展用于创建、测试函数以及将函数部署到 Azure。The Azure Functions extension is used to create, test, and deploy functions to Azure.

  1. 在 Visual Studio Code 中打开“扩展”并搜索 azure functions,或者在 Visual Studio Code 中打开此链接In Visual Studio Code, open Extensions and search for azure functions, or open this link in Visual Studio Code.

  2. 选择“安装”将扩展安装到 Visual Studio Code。Select Install to install the extension to Visual Studio Code.

    安装适用于 Azure Functions 的扩展

  3. 重新启动 Visual Studio Code,然后选择“活动”栏上的 Azure 图标。Restart Visual Studio Code and select the Azure icon on the Activity bar. 此时侧边栏中应显示 Azure Functions 区域。You should see an Azure Functions area in the Side Bar.

    侧边栏中的 Azure Functions 区域

创建 Azure Functions 项目Create an Azure Functions project

使用 Visual Studio Code 中的 Azure Functions 项目模板可创建一个项目,该项目可发布到 Azure 中的函数应用。The Azure Functions project template in Visual Studio Code creates a project that can be published to a function app in Azure. 函数应用可将函数分组为一个逻辑单元,以用于管理、部署和共享资源。A function app lets you group functions as a logical unit for management, deployment, and sharing of resources.

  1. 在 Visual Studio Code 中,选择 Azure 徽标以显示“Azure: Functions”区域,然后选择“创建新项目”图标。In Visual Studio Code, select the Azure logo to display the Azure: Functions area, and then select the Create New Project icon.

    创建函数应用项目

  2. 为项目工作区选择位置,然后选择“选择”。Choose a location for your project workspace and choose Select.

    备注

    本文已设计为在工作区之外完成。This article was designed to be completed outside of a workspace. 在这种情况下,请不要选择属于工作区内的项目文件夹。In this case, do not select a project folder that is part of a workspace.

  3. 选择函数应用项目的语言。Select the language for your function app project. 在本文中将使用 JavaScript。In this article, JavaScript is used. 选择项目语言Choose project language

  4. 出现提示时,选择“添加到工作区”。When prompted, choose Add to workspace.

Visual Studio Code 将在新的工作区中创建函数应用项目。Visual Studio Code creates the function app project in a new workspace. 此项目包含 host.jsonlocal.settings.json 配置文件以及任何特定于语言的项目文件。This project contains the host.json and local.settings.json configuration files, plus any language-specific project files. 在项目文件夹中还会有新的 Git 存储库。You also get a new Git repository in the project folder.

创建 HTTP 触发的函数Create an HTTP triggered function

  1. Azure:Functions 区域中,选择“创建函数”图标。From Azure: Functions, choose the Create Function icon.

    创建函数

  2. 选择包含函数应用项目的文件夹,然后选择“HTTP 触发器”函数模板。Select the folder with your function app project and select the HTTP trigger function template.

    选择 HTTP 触发器模板

  3. 键入 HTTPTrigger 作为函数名称,然后按 Enter,然后选择“匿名”身份验证。Type HTTPTrigger for the function name and press Enter, then select Anonymous authentication.

    选择匿名身份验证

    此时将使用 HTTP 触发的函数的模板,以所选语言创建函数。A function is created in your chosen language using the template for an HTTP-triggered function.

    Visual Studio Code 中的 HTTP 触发的函数模板

可以通过修改 function.json 文件,将输入和输出绑定添加到函数。You can add input and output bindings to your function by modifying the function.json file. 有关详细信息,请参阅 Azure Functions 触发器和绑定概念For more information, see Azure Functions triggers and bindings concepts.

创建函数项目和 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 Code 启动某个函数时,系统会提示你安装这些工具。You're prompted to install these tools the first time you start a function from Visual Studio Code.

  1. 若要测试函数,请在函数代码中设置断点并按 F5 启动函数应用项目。To test your function, set a breakpoint in the function code and press F5 to start the function app project. 来自 Core Tools 的输出会显示在“终端”面板中。Output from Core Tools is displayed in the Terminal panel.

  2. 在“终端”面板中,复制 HTTP 触发的函数的 URL 终结点。In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.

    Azure 本地输出

  3. 将 HTTP 请求的 URL 粘贴到浏览器的地址栏中。Paste the URL for the HTTP request into your browser's address bar. 将查询字符串 ?name=<yourname> 追加到此 URL 并执行请求。Append the query string ?name=<yourname> to this URL and execute the request. 执行将在命中断点时暂停。Execution is paused when the breakpoint is hit.

    Visual Studio Code 中的命中断点的函数

  4. 下面显示了当继续执行时浏览器中对 GET 请求的响应:When you continue the execution, the following shows the response in the browser to the GET request:

    浏览器中的函数 localhost 响应

  5. 若要停止调试,请按 Shift + F5。To stop debugging, press Shift + F5.

确认该函数可以在本地计算机上正确运行以后,即可将项目发布到 Azure。After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

登录 AzureSign in to Azure

在发布应用之前,必须先登录到 Azure。Before you can publish your app, you must sign in to Azure.

  1. 在“Azure: Functions”区域中,选择“登录到 Azure...”。如果没有帐户,可以创建一个免费 Azure 帐户In the Azure: Functions area, choose Sign in to Azure.... If you don't already have one, you can Create a free Azure account.

    浏览器中的函数 localhost 响应

  2. 出现提示时,选择“复制并打开”,或者复制显示的代码并在浏览器中打开 https://aka.ms/deviceloginWhen prompted, select Copy & Open, or copy the displayed code and open https://aka.ms/devicelogin in your browser.

  3. 将复制的代码粘贴到“设备登录”页中,验证 Visual Studio Code 的登录信息,然后选择“继续”。Paste the copied code in the Device Login page, verify the sign in for Visual Studio Code, then select Continue.

  4. 使用 Azure 帐户凭据完成登录。Complete the sign in using your Azure account credentials. 成功登录后,可以关闭浏览器。After you have successfully signed in, you can close the browser.

将项目发布到 AzurePublish the project to Azure

使用 Visual Studio Code 可以将函数项目直接发布到 Azure。Visual Studio Code lets you publish your functions project directly to Azure. 在此过程中,将在 Azure 订阅中创建函数应用和相关的资源。In the process, you create a function app and related resources in your Azure subscription. 函数应用为函数提供了执行上下文。The function app provides an execution context for your functions. 该项目将打包并部署到 Azure 订阅中的新函数应用。The project is packaged and deployed to the new function app in your Azure subscription.

本文假定你要创建新的函数应用。This article assumes that you are creating a new function app. 发布到现有函数应用将覆盖该应用在 Azure 中的内容。Publishing to an existing function app overwrites the content of that app in Azure.

  1. 在“Azure: Functions”区域中,选择“部署到函数应用”图标。In the Azure: Functions area, select the Deploy to Function App icon.

    函数应用设置

  2. 选择项目文件夹(即当前工作区)。Choose the project folder, which is your current workspace.

  3. 如果有多个订阅,请选择要承载函数应用的订阅,然后选择“+ 创建新的函数应用”。If you have more than one subscription, choose the one you want to host your function app, then choose + Create New Function App.

  4. 键入用于标识函数应用的全局唯一名称,然后按 Enter。Type a globally unique name that identifies your function app and press Enter. 函数应用名称的有效字符包括 a-z0-9-Valid characters for a function app name are a-z, 0-9, and -.

  5. 选择“+ 创建新的资源组”,键入资源组名称(如 myResourceGroup),然后按 Enter。Choose + Create New Resource Group, type a resource group name, like myResourceGroup, and press enter. 也可以使用现有资源组。You can also use an existing resource group.

  6. 选择“+ 新建存储帐户”,键入函数应用使用的新存储帐户的全局唯一名称,然后按 Enter。Choose + Create New Storage Account, type a globally unique name of the new storage account used by your function app and press Enter. 存储帐户名称必须为 3 到 24 个字符,并且只能包含数字和小写字母。Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. 也可以使用现有帐户。You can also use an existing account.

  7. 选择离你近或离函数访问的其他服务近的区域中的位置。Choose a location in a region near you or near other services your functions access.

    选择位置后,创建函数应用将会开始。Function app creation starts after you choose your location. 创建函数应用并应用了部署包之后,会显示一个通知。A notification is displayed after your function app is created and the deployment package is applied.

  8. 在通知中选择“查看输出”以查看创建和部署结果,其中包括你创建的 Azure 资源。Select View Output in the notifications to view the creation and deployment results, including the Azure resources that you created.

    函数应用创建输出

  9. 记下 Azure 中的新函数应用的 URL。Make a note of the URL of the new function app in Azure. 在该项目发布到 Azure 之后将使用此 URL 来测试函数。You use this to test your function after the project is published to Azure.

    函数应用创建输出

  10. 回到“Azure: Functions”区域中,你将看到订阅下显示了新的函数应用。Back in the Azure: Functions area, you see the new function app displayed under your subscription. 展开此节点时,可以看到函数应用中的函数,以及应用程序设置和函数代理。When you expand this node, you see the functions in the function app, as well as application settings and function proxies.

    函数应用设置

    从函数应用节点中,按住 Ctrl 并单击(右键单击)可选择要在 Azure 中针对该函数应用执行的各种管理和配置任务。From your function app node, type Ctrl and click (right-click) to choose to perform various management and configuration tasks against the function app in Azure. 还可以选择在 Azure 门户中查看该函数应用。You can also choose to view the function app in the Azure portal.

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

  1. 从“输出”面板复制 HTTP 触发器的 URL。Copy the URL of the HTTP trigger from the Output panel. 与前面一样,请确保将查询字符串 ?name=<yourname> 添加到此 URL 的末尾并执行请求。As before, make sure to add the query string ?name=<yourname> to the end of this URL and execute the request.

    调用 HTTP 触发的函数的 URL 应采用以下格式:The URL that calls your HTTP-triggered function should be in the following format:

     http://<functionappname>.azurewebsites.net/api/<functionname>?name=<yourname> 
    
  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 Code 通过简单的 HTTP 触发函数创建了函数应用。You have used Visual Studio Code to create a function app with a simple HTTP-triggered function. 你还可能想要了解有关使用 Azure Functions Core Tools 从终端或命令提示符进行本地测试和调试的详细信息。You may also want to learn more about local testing and debugging from the Terminal or command prompt using the Azure Functions Core Tools.