您现在访问的是微软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 supports C#, JavaScript, and Java functions, with Python support currently in Preview. 本文和后续文章中的步骤仅支持 JavaScript 和 C# 函数。The steps in this article and the article that follows support only JavaScript and C# functions. 若要了解如何使用 Visual Studio Code 创建和发布 Python 函数,请参阅将 Python 部署到 Azure FunctionsTo learn how to use Visual Studio Code to create and publish Python functions, see Deploy Python to Azure Functions. 若要了解如何使用 Visual Studio Code 创建和发布 PowerShell 函数,请参阅在 Azure 中创建第一个 PowerShell 函数To learn how to use Visual Studio Code to create and publish PowerShell functions, see Create your first PowerShell function in Azure.

此扩展目前为预览版。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 Functions extension

可以使用 Azure Functions 扩展创建和测试函数,并将其部署到 Azure。You can use the Azure Functions extension to create and test functions and deploy them to Azure.

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

  2. 选择“安装” 以安装 Visual Studio Code 的扩展:Select Install to install the extension for 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 区域

创建包含一个函数的 Functions 项目Create your Functions project with a function

使用 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 中,按 F1 键打开命令面板。In Visual Studio Code, press F1 to open the command palette. 在命令面板中,搜索并选择 Azure Functions: Create new project...In the command palette, search for and select Azure Functions: Create new project....

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

    备注

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

  3. 按照提示提供以下信息:Following the prompts, provide the following information:

    PromptPrompt Value 说明Description
    选择函数应用项目的语言Select a language for your function app project C# 或 JavaScriptC# or JavaScript 本文支持 C# 和 JavaScript。This article supports C# and JavaScript. 对于 Python,请参阅此 Python 文章,对于 PowerShell,请参阅此 PowerShell 文章For Python, see this Python article, and for PowerShell, see this PowerShell article.
    为项目的第一个函数选择模板Select a template for your project's first function HTTP 触发器HTTP trigger 在新的函数应用中创建一个 HTTP 触发的函数。Create an HTTP triggered function in the new function app.
    提供函数名称Provide a function name HttpTriggerHttpTrigger 按 Enter 以使用默认名称。Press Enter to use the default name.
    提供命名空间Provide a namespace My.FunctionsMy.Functions (仅 C#)C# 类库必须具有命名空间。(C# only) C# class libraries must have a namespace.
    授权级别Authorization level 函数Function 需要函数密钥才能调用函数的 HTTP 终结点。Requires a function key to call the function's HTTP endpoint.
    选择打开项目的方式Select how you would like to open your project 添加到工作区Add to workspace 在当前工作区中创建函数应用。Creates the function app in the current 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.

还会在函数应用项目的 HttpTrigger 文件夹中创建一个新的 HTTP 触发函数。A new HTTP triggered function is also created in the HttpTrigger folder of the function app project.

在本地运行函数Run 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.

  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. 此 URL 包括函数密钥,该密钥将传递给 code 查询参数。This URL includes the function key, which is passed to the code query parameter.

    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.

  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:函数”区域中,选择“登录到 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.

默认情况下,Visual Studio Code 会创建函数应用创建所需的所有 Azure 资源。By default, Visual Studio Code creates all of the Azure resources required to create your function app. 这些资源的名称基于所选的函数应用名称。The names of these resources are based on the function app name you choose. 如果需要完全控制已创建的资源,则可以改为使用高级选项发布If you need to have full control of the created resources, you can instead publish using advanced options.

本部分假设你正在 Azure 中创建新的函数应用。This section assumes that you are creating a new function app in Azure.

重要

发布到现有函数应用将覆盖该应用在 Azure 中的内容。Publishing to an existing function app overwrites the content of that app in Azure.

  1. 在 Visual Studio Code 中,按 F1 键打开命令面板。In Visual Studio Code, press F1 to open the command palette. 在命令面板中,搜索并选择 Azure Functions: Deploy to function app...In the command palette, search for and select Azure Functions: Deploy to function app....

  2. 如果未登录,则系统会提示你登录到 AzureIf not signed-in, you are prompted to Sign in to Azure. 还可以创建免费 Azure 帐户You can also Create a free Azure account. 在成功从浏览器登录后,返回到 Visual Studio Code。After successful sign in from the browser, go back to Visual Studio Code.

  3. 如果你有多个订阅,请为函数应用选择一个订阅,然后选择“+ 在 Azure 中创建新的函数应用” 。If you have multiple subscriptions, Select a subscription for the function app, then choose + Create New Function App in Azure.

  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 -.

    按 Enter 时,将在你的订阅中创建以下 Azure 资源:When you press Enter, the following Azure resources are created in your subscription:

    • 资源组 :包含创建的所有 Azure 资源。Resource group: Contains all of the created Azure resources. 名称基于你的函数应用名称。The name is based on your function app name.
    • 存储帐户 :将使用基于你的函数应用名称的唯一名称创建一个标准存储帐户。Storage account: A standard Storage account is created with a unique name that is based on your function app name.
    • 托管计划 :将在美国西部区域中创建一个消耗计划来托管你的无服务器函数应用。Hosting plan: A consumption plan is created in the West US region to host your serverless function app.
    • 函数应用:你的项目将部署到此新的函数应用并在其中运行。Function app: Your project is deployed to and runs in this new function app.

    创建函数应用并应用了部署包之后,会显示一个通知。A notification is displayed after your function app is created and the deployment package is applied. 在此通知中选择“查看输出” 以查看创建和部署结果,其中包括你创建的 Azure 资源。Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created.

  5. 返回到“Azure:函数” 区域,在你的订阅下展开新的函数应用。Back in the Azure: Functions area, expand the new function app under your subscription. 展开“函数” ,右键单击“HttpTrigger” ,然后选择“复制函数 URL” 。Expand Functions, right-click HttpTrigger, and then choose Copy function URL.

    复制新的 HTTP 触发器的函数 URL

在 Azure 中运行函数Run the 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. 在下一篇文章中,将通过添加输出绑定来扩展该函数。In the next article, you expand that function by adding an output binding. 此绑定将 HTTP 请求中的字符串写入 Azure 队列存储队列中的消息。This binding writes the string from the HTTP request to a message in an Azure Queue Storage queue. 下一篇文章还演示了如何通过删除所创建的资源组来清理这些新的 Azure 资源。The next article also shows you how to clean up these new Azure resources by removing the resource group you created.