visualstudio编写java_使用 Visual Studio Code 创建 C# 函数 - Azure Functions | Microsoft Docs

本教程指导如何使用Visual Studio Code创建并部署一个响应HTTP请求的C# Azure函数。首先,配置环境,包括安装必备软件。接着,通过VS Code生成C#函数项目,创建HTTP触发器函数。然后,本地运行并测试函数,确保其正常工作。最后,登录Azure,将项目直接发布到Azure,创建相关资源,并在Azure中运行函数。完成教程后,你将了解如何使用VS Code开发和部署Azure函数。
摘要由CSDN通过智能技术生成

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

快速入门:在 Azure 中使用 Visual Studio Code 创建 C# 函数Quickstart: Create a C# function in Azure using Visual Studio Code

11/03/2020

本文内容

在本文中,我们使用 Visual Studio Code 来创建一个响应 HTTP 请求的基于 C# 类库的函数。In this article, you use Visual Studio Code 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.

There's also a CLI-based version of this article.

配置环境Configure your environment

在开始之前,请确保已满足下列要求:Before you get started, make sure you have the following requirements in place:

创建本地项目Create your local project

在本部分,你将使用 Visual Studio Code 在 C# 中创建一个本地 Azure Functions 项目。In this section, you use Visual Studio Code to create a local Azure Functions project in C#. 稍后在本文中,你要将函数代码发布到 Azure。Later in this article, you'll publish your function code to Azure.

在活动栏中选择“Azure”图标,然后在“Azure:函数”区域中选择“创建新项目...”图标。Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project... icon.

3da2e6f670336b61cf321cde3a6f6b51.png

为项目工作区选择目录位置,然后选择“选择” 。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.

根据提示提供以下信息:Provide the following information at the prompts:

选择函数项目的语言:选择C#。Select a language for your function project: Choose C#.

为项目的第一个函数选择模板:选择HTTP trigger。Select a template for your project's first function: Choose HTTP trigger.

提供函数名称:键入 HttpExample。Provide a function name: Type HttpExample.

提供命名空间:键入 My.Functions。Provide a namespace: Type My.Functions.

授权级别:选择 Anonymous,这使任何人都可以调用你的函数终结点。Authorization level: Choose Anonymous, which enables anyone to call your function endpoint. 若要了解授权级别,请参阅授权密钥。To learn about authorization level, see Authorization keys.

选择打开项目的方式:选择Add to workspace。Select how you would like to open your project: Choose Add to workspace.

Visual Studio Code 将使用此信息生成一个包含 HTTP 触发器的 Azure Functions 项目。Using this information, Visual Studio Code generates an Azure Functions project with an HTTP trigger. 可以在资源管理器中查看本地项目文件。You can view the local project files in the Explorer. 若要详细了解所创建的文件,请参阅生成的项目文件。To learn more about files that are created, see Generated project files.

在本地运行函数Run the function locally

Visual Studio Code 与 Azure Functions Core Tools 相集成,便于你在发布到 Azure 之前在本地开发计算机上运行此项目。Visual Studio Code integrates with Azure Functions Core tools to let you run this project on your local development computer before you publish to Azure.

若要调用函数,请按 F5 启动函数应用项目。To call your function, press F5 to start the function app project. 来自 Core Tools 的输出会显示在“终端” 面板中。Output from Core Tools is displayed in the Terminal panel. 应用将在“终端”面板中启动。Your app starts in the Terminal panel. 可以看到 HTTP 触发函数的 URL 终结点在本地运行。You can see the URL endpoint of your HTTP-triggered function running locally.

cce5658ebc06e7f61cd18dc0cb1300bf.png

如果在 Windows 上运行时遇到问题,请确保用于 Visual Studio Code 的默认终端未设置为“WSL Bash”。If you have trouble running on Windows, make sure that the default terminal for Visual Studio Code isn't set to WSL Bash.

运行 Core Tools 后,转到“Azure:Functions”区域。With Core Tools running, go to the Azure: Functions area. 在“Functions”下,展开“本地项目” > “Functions” 。Under Functions, expand Local Project > Functions. 右键单击 (Windows) 或按 Ctrl - 单击 (macOS) HttpExample 函数,然后选择“立即执行函数...”。Right-click (Windows) or Ctrl - click (macOS) the HttpExample function and choose Execute Function Now....

6219c0200feb6b37a46b06e934169884.png

在“输入请求正文”中,你将看到请求消息正文值 { "name": "Azure" }。In Enter request body you see the request message body value of { "name": "Azure" }. 按 Enter 将此请求消息发送给函数。Press Enter to send this request message to your function.

当函数在本地执行并返回响应时,Visual Studio Code 中将引发通知。When the function executes locally and returns a response, a notification is raised in Visual Studio Code. 函数执行的相关信息将显示在“终端”面板中。Information about the function execution is shown in Terminal panel.

按 Ctrl + C 停止 Core Tools 并断开调试器的连接。Press Ctrl + C to stop Core Tools and disconnect the debugger.

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

登录 AzureSign in to Azure

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

如果你尚未登录,请在活动栏中选择“Azure”图标,然后在“Azure: 函数”区域中,选择“登录到 Azure...”。如果没有帐户,可以创建一个免费 Azure 帐户。If you aren't already signed in, choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose Sign in to Azure.... If you don't already have one, you can

c4962ac77b7b1979676ded90b6b1a650.png

如果已登录,请转到下一部分。If you're already signed in, go to the next section.

在浏览器中出现提示时,请选择你的 Azure 帐户,并使用你的 Azure 帐户凭据登录。When prompted in the browser, choose your Azure account and sign in using your Azure account credentials.

成功登录后,可以关闭新浏览器窗口。After you've successfully signed in, you can close the new browser window. 属于你的 Azure 帐户的订阅显示在边栏中。The subscriptions that belong to your Azure account are displayed in the Side bar.

将项目发布到 AzurePublish the project to Azure

在本部分,你将在 Azure 订阅中创建一个函数应用和相关资源,然后部署代码。In this section, you create a function app and related resources in your Azure subscription and then deploy your code.

重要

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

在活动栏中选择“Azure”图标,然后在“Azure:函数”区域中,选择“部署到函数应用...”按钮。Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button.

8b1063b9938af26708c0450844528e29.png

根据提示提供以下信息:Provide the following information at the prompts:

选择文件夹:从工作区中选择一个文件夹,或浏览到包含函数应用的文件夹。Select folder: Choose a folder from your workspace or browse to one that contains your function app. 如果已打开有效的函数应用,则不会看到此信息。You won't see this if you already have a valid function app opened.

选择订阅:选择要使用的订阅。Select subscription: Choose the subscription to use. 如果只有一个订阅,则不会看到此项。You won't see this if you only have one subscription.

在 Azure 中选择函数应用:选择- Create new Function App。Select Function App in Azure: Choose - Create new Function App. (请不要选择本文中未介绍的 Advanced 选项。)(Don't choose the Advanced option, which isn't covered in this article.)

输入函数应用的全局唯一名称:键入在 URL 路径中有效的名称。Enter a globally unique name for the function app: Type a name that is valid in a URL path. 将对你键入的名称进行验证,以确保其在 Azure Functions 中是唯一的。The name you type is validated to make sure that it's unique in Azure Functions.

选择新资源的位置:为了获得更好的性能,请选择你附近的 区域。Select a location for new resources: For better performance, choose a region near you.

在 Azure 中创建单个资源时,扩展会在通知区域显示这些资源的状态。The extension shows the status of individual resources as they are being created in Azure in the notification area.

75ec65293ede62f548919fb759f62e79.png

完成后,将使用基于函数应用名称的名称在订阅中创建以下 Azure 资源:When completed, the following Azure resources are created in your subscription, using names based on your function app name:

一个资源组:相关资源的逻辑容器。A resource group, which is a logical container for related resources.

一个标准 Azure 存储帐户:用于维护项目的状态和其他信息。A standard Azure Storage account, which maintains state and other information about your projects.

一个消耗计划:用于定义无服务器函数应用的基础主机。A consumption plan, which defines the underlying host for your serverless function app.

一个函数应用:提供用于执行函数代码的环境。A function app, which provides the environment for executing your function code. 可以通过函数应用将函数分组为逻辑单元,以便在同一托管计划中更轻松地管理、部署和共享资源。A function app lets you group functions as a logical unit for easier management, deployment, and sharing of resources within the same hosting plan.

一个连接到函数应用的 Application Insights 实例:用于跟踪无服务器函数的使用情况。An Application Insights instance connected to the function app, which tracks usage of your serverless function.

创建函数应用并应用了部署包之后,会显示一个通知。A notification is displayed after your function app is created and the deployment package is applied.

提示

默认情况下,会根据你提供的函数应用名称创建函数应用所需的 Azure 资源。By default, the Azure resources required by your function app are created based on the function app name you provide. 默认情况下,还会在函数应用所在的新资源组中创建这些资源。By default, they are also created in the same new resource group with the function app. 如果要自定义这些资源的名称或重复使用现有资源,则需要使用高级创建选项来发布项目。If you want to either customize the names of these resources or reuse existing resources, you need to instead publish the project with advanced create options.

在此通知中选择“查看输出”以查看创建和部署结果,其中包括你创建的 Azure 资源。Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created. 如果错过了通知,请选择右下角的响铃图标以再次查看。If you miss the notification, select the bell icon in the lower right corner to see it again.

8f085f4eec5f5ef8ddb5ef7a4fb2712b.png

在 Azure 中运行函数Run the function in Azure

返回到“Azure:函数”区域(位于侧栏中),然后展开你的订阅、新的函数应用和“函数”。Back in the Azure: Functions area in the side bar, expand your subscription, your new function app, and Functions. 右键单击 (Windows) 或按 Ctrl - 单击 (macOS) HttpExample 函数,然后选择“立即执行函数...”。Right-click (Windows) or Ctrl - click (macOS) the HttpExample function and choose Execute Function Now....

3521e1949c2ee936d9eeccaebbfd0960.png

在“输入请求正文”中,你将看到请求消息正文值 { "name": "Azure" }。In Enter request body you see the request message body value of { "name": "Azure" }. 按 Enter 将此请求消息发送给函数。Press Enter to send this request message to your function.

当函数在 Azure 中执行并返回响应时,Visual Studio Code 中将引发通知。When the function executes in Azure and returns a response, a notification is raised in Visual Studio Code.

清理资源Clean up resources

若要继续执行下一步骤将 Azure 存储队列绑定添加到函数,需要保留到目前为止构建的所有资源。When you continue to the next step and add an Azure Storage queue binding to your function, you'll need to keep all your resources in place to build on what you've already done.

否则,可以使用以下步骤删除函数应用及其相关资源,以免产生任何额外的费用。Otherwise, you can use the following steps to delete the function app and its related resources to avoid incurring any further costs.

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

选择你的函数应用,然后按 Enter。Choose your function app, and press Enter. 随即将在 Azure 门户中打开函数应用页面。The function app page opens in the Azure portal.

在“概览”选项卡中,选择“资源组”旁边的命名链接。In the Overview tab, select the named link next to Resource group.

fa26c4e20357ce6c5efc64709bc408ac.png

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

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

若要详细了解 Functions 的费用,请参阅估算消耗计划成本。To learn more about Functions costs, see Estimating Consumption plan costs.

后续步骤Next steps

你已使用 Visual Studio Code 通过简单的 HTTP 触发函数创建了函数应用。You have used Visual Studio Code to create a function app with a simple HTTP-triggered function. 在下一篇文章中,你将通过连接到 Azure 存储来扩展该函数。In the next article, you expand that function by connecting to Azure Storage. To learn more about connecting to other Azure services, see Add bindings to an existing function in Azure Functions.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值