使用 C# 创建你的第一个持久函数Create your first durable function in C#

Durable FunctionsAzure Functions 的一个扩展,可用于在无服务器环境中编写有状态函数。Durable Functions is an extension of Azure Functions that lets you write stateful functions in a serverless environment. 该扩展可用于管理状态、检查点和重启。The extension manages state, checkpoints, and restarts for you.

本文介绍了如何使用 Visual Studio Code 在本地创建并测试“hello world”持久函数。In this article, you learn how to use Visual Studio Code to locally create and test a "hello world" durable function. 此函数将协调对其他函数的调用并将其链接在一起。This function orchestrates and chains-together calls to other functions. 然后将函数代码发布到 Azure。You then publish the function code to Azure. 这些工具作为 VS Code Azure Functions 扩展的一部分提供。These tools are available as part of the VS Code Azure Functions extension.

在 Azure 中运行持久函数

必备条件Prerequisites

完成本教程:To complete this tutorial:

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

创建本地项目Create your local project

在本部分,你将使用 Visual Studio Code 创建一个本地 Azure Functions 项目。In this section, you use Visual Studio Code to create a local Azure Functions project.

  1. 在 Visual Studio Code 中,按 F1(或 Ctrl/命令键+Shift+P)打开命令面板。In Visual Studio Code, press F1 (or Ctrl/Cmd+Shift+P) 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 an empty folder location for your project and choose Select.

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

    PromptPrompt Value 说明Description
    选择函数应用项目的语言Select a language for your function app project C#C# 创建本地 C# Functions 项目。Create a local C# Functions project.
    选择版本Select a version Azure Functions v3Azure Functions v3 仅当尚未安装 Core Tools 时,才会出现此选项。You only see this option when the Core Tools aren't already installed. 在本例中,当你首次运行应用时即已安装 Core Tools。In this case, Core Tools are installed the first time you run the app.
    为项目的第一个函数选择模板Select a template for your project's first function 暂时跳过Skip for now
    选择打开项目的方式Select how you would like to open your project 在当前窗口中打开Open in current window 在所选的文件夹中重新打开 VS Code。Reopens VS Code in the folder you selected.

Visual Studio Code 会根据需要安装 Azure Functions Core Tools。Visual Studio Code installs the Azure Functions Core Tools, if needed. 它还会在某个文件夹中创建一个函数应用项目。It also creates a function app project in a folder. 此项目包含 host.jsonlocal.settings.json 配置文件。This project contains the host.json and local.settings.json configuration files.

向应用中添加函数Add functions to the app

以下步骤使用模板在项目中创建持久函数代码。The following steps use a template to create the durable function code in your project.

  1. 在命令面板中,搜索并选择 Azure Functions: Create Function...In the command palette, search for and select Azure Functions: Create Function....

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

    PromptPrompt Value 说明Description
    选择函数的模板Select a template for your function DurableFunctionsOrchestrationDurableFunctionsOrchestration 创建 Durable Functions 业务流程Create a Durable Functions orchestration
    提供函数名称Provide a function name HelloOrchestrationHelloOrchestration 在其中创建函数的类的名称Name of the class in which functions are created
    提供命名空间Provide a namespace Company.FunctionCompany.Function 所生成类的命名空间Namespace for the generated class
  3. 当 VS Code 提示选择存储帐户时,请选择“选择存储帐户”。When VS Code prompts you to select a storage account, choose Select storage account. 按照提示提供以下信息,以在 Azure 中创建新的存储帐户。Following the prompts, provide the following information to create a new storage account in Azure.

    PromptPrompt Value 说明Description
    选择订阅Select subscription 订阅的名称name of your subscription 选择 Azure 订阅Select your Azure subscription
    选择存储帐户Select a storage account 新建存储帐户Create a new storage account
    输入新存储帐户的名称Enter the name of the new storage account 唯一名称unique name 要创建的存储帐户的名称Name of the storage account to create
    选择资源组Select a resource group 唯一名称unique name 要创建的资源组名称Name of the resource group to create
    选择位置Select a location regionregion 选择离你较近的区域Select a region close to you

包含新函数的类会添加到此项目。A class containing the new functions is added to the project. VS Code 还将存储帐户连接字符串添加到 local.settings.json,并将对 Microsoft.Azure.WebJobs.Extensions.DurableTask NuGet 包的引用添加到 .csproj 项目文件。VS Code also adds the storage account connection string to local.settings.json and a reference to the Microsoft.Azure.WebJobs.Extensions.DurableTask NuGet package to the .csproj project file.

打开新的 HelloOrchestration.cs 文件,查看其内容。Open the new HelloOrchestration.cs file to view the contents. 此持久函数是一个简单的函数链接示例,包含以下方法:This durable function is a simple function chaining example with the following methods:

方法Method FunctionNameFunctionName 说明Description
RunOrchestrator HelloOrchestration 管理持久业务流程。Manages the durable orchestration. 在此示例中,业务流程启动,创建一个列表,并将三个函数调用的结果添加到列表中。In this case, the orchestration starts, creates a list, and adds the result of three functions calls to the list. 当三个函数调用完成后,它返回该列表。When the three function calls are complete, it returns the list.
SayHello HelloOrchestration_Hello 此函数返回一个 hello。The function returns a hello. 函数包含要协调的业务逻辑。It is the function that contains the business logic that is being orchestrated.
HttpStart HelloOrchestration_HttpStart HTTP 触发的函数,用于启动业务流程的实例并返回检查状态响应。An HTTP-triggered function that starts an instance of the orchestration and returns a check status response.

现在,你已创建了函数项目和一个持久函数,可以在本地计算机上对其进行测试。Now that you've created your function project and a durable 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. 若要测试函数,请在 SayHello 活动函数代码中设置断点,然后按 F5 启动函数应用项目。To test your function, set a breakpoint in the SayHello activity function code and press F5 to start the function app project. 来自 Core Tools 的输出会显示在“终端”面板中。Output from Core Tools is displayed in the Terminal panel.

    备注

    有关调试的详细信息,请参阅 Durable Functions 诊断Refer to the Durable Functions Diagnostics for more information on debugging.

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

    Azure 本地输出

  3. 使用 PostmancURL 之类的工具向 URL 终结点发送一个 HTTP POST 请求。Using a tool like Postman or cURL, send an HTTP POST request to the URL endpoint.

    响应是来自 HTTP 函数的初始结果,让我们知道持久业务流程已成功启动。The response is the initial result from the HTTP function letting us know the durable orchestration has started successfully. 它还不是业务流程的最终结果。It is not yet the end result of the orchestration. 响应中包括了几个有用的 URL。The response includes a few useful URLs. 现在,让我们查询业务流程的状态。For now, let's query the status of the orchestration.

  4. 复制 statusQueryGetUri 的 URL 值,将其粘贴到浏览器的地址栏中并执行请求。Copy the URL value for statusQueryGetUri and paste it in the browser's address bar and execute the request. 或者也可以继续使用 Postman 发出 GET 请求。Alternatively you can also continue to use Postman to issue the GET request.

    请求将查询业务流程实例的状态。The request will query the orchestration instance for the status. 你应该会得到一个最终响应,它显示实例已经完成,并包含持久函数的输出或结果。You should get an eventual response, which shows us the instance has completed, and includes the outputs or results of the durable function. 输出如下所示:It looks like:

    {
        "name": "HelloOrchestration",
        "instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
        "runtimeStatus": "Completed",
        "input": null,
        "customStatus": null,
        "output": [
            "Hello Tokyo!",
            "Hello Seattle!",
            "Hello London!"
        ],
        "createdTime": "2020-03-18T21:54:49Z",
        "lastUpdatedTime": "2020-03-18T21:54:54Z"
    }
    
  5. 若要停止调试,请在 VS Code 中按 Shift + F5To stop debugging, press Shift + F5 in VS Code.

确认该函数可以在本地计算机上正确运行以后,即可将项目发布到 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. 打开 Visual Studio Code,然后单击 View -> Command Palette...Open Visual Studio Code, click View -> Command Palette....

  2. 输入 Sign In to Azure Cloud,以搜索 Azure 中国环境。Input Sign In to Azure Cloud to search Azure China Environment.

  3. 在搜索结果中单击 Azure ChinaClick Azure China in search result.

  4. 窗口的右下角将出现一个弹出窗口。You will find a popup window at the bottom right of your window.

    弹出窗口

  5. 打开页面 **https://microsoft.com/deviceloginchina**,然后输入该代码进行身份验证。Open the page https://microsoft.com/deviceloginchina and enter the code to authenticate.

将项目发布到 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.

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

    将项目发布到 Azure

  2. 根据提示提供以下信息: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 AppSelect 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.

    • 选择运行时:选择你一直在本地运行的 Node.js 版本。Select a runtime: Choose the version of Node.js you've been running on locally. 可以使用 node --version 命令来检查你的版本。You can use the node --version command to check your version.
    • 选择新资源的位置:为了获得更好的性能,请选择你附近的区域Select a location for new resources: For better performance, choose a region near you.
  3. 完成后,将使用基于函数应用名称的名称在订阅中创建以下 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.

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

  4. 在此通知中选择“查看输出”以查看创建和部署结果,其中包括你创建的 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.

    创建完成通知

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

  1. 从“输出” 面板复制 HTTP 触发器的 URL。Copy the URL of the HTTP trigger from the Output panel. 调用 HTTP 触发的函数的 URL 应采用以下格式:The URL that calls your HTTP-triggered function should be in the following format:

    https://<functionappname>.chinacloudsites.cn/api/HelloOrchestration_HttpStart

  2. 将 HTTP 请求的这个新 URL 粘贴到浏览器的地址栏中。Paste this new URL for the HTTP request into your browser's address bar. 你应当会得到与之前使用已发布的应用时相同的状态响应。You should get the same status response as before when using the published app.

后续步骤Next steps

你已使用 Visual Studio Code 创建并发布了一个 C# 持久函数应用。You have used Visual Studio Code to create and publish a C# durable function app.

本文介绍了如何使用 Visual Studio 2019 在本地创建并测试“hello world”持久函数。In this article, you learn how to Visual Studio 2019 to locally create and test a "hello world" durable function. 此函数将协调对其他函数的调用并将其链接在一起。This function orchestrates and chains-together calls to other functions. 然后将函数代码发布到 Azure。You then publish the function code to Azure. Visual Studio 2019 中的 Azure 开发工作负荷已随附这些工具。These tools are available as part of the Azure development workload in Visual Studio 2019.

在 Azure 中运行持久函数

必备条件Prerequisites

完成本教程:To complete this tutorial:

  • 安装 Visual Studio 2019Install Visual Studio 2019. 确保还安装了 Azure 开发工作负荷。Make sure that the Azure development workload is also installed. Visual Studio 2017 也支持 Durable Functions 开发,但 UI 和步骤不同。Visual Studio 2017 also supports Durable Functions development, but the UI and steps differ.

  • 验证 Azure 存储模拟器是否已安装且正在运行。Verify you have the Azure Storage Emulator installed and running.

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

创建函数应用项目Create a function app project

Azure Functions 模板创建一个项目,该项目可发布到 Azure 中的函数应用。The Azure Functions template 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 easier management, deployment, scaling, and sharing of resources.

  1. 在 Visual Studio 中,从“文件”菜单中选择“新建” > “项目”。In Visual Studio, select New > Project from the File menu.

  2. 在“创建新项目”对话框中,搜索 functions,选择“Azure Functions”模板,然后选择“下一步”。In the Create a new project dialog, search for functions, choose the Azure Functions template, and select Next.

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

  3. 为项目键入项目名称,然后选择“确定”。Type a Project name for your project, and select OK. 项目名称必须可以充当 C# 命名空间,因此请勿使用下划线、连字符或任何其他的非字母数字字符。The project 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 settings specified in the table that follows the image.

    Visual Studio 中的“新建 Azure Functions 应用程序”对话框

    设置Setting 建议的值Suggested value 说明Description
    版本Version Azure Functions 3.0Azure Functions 3.0
    (.NET Core)(.NET Core)
    创建一个函数项目,并让其使用 Azure Functions 的版本 3.0 运行时(支持 .NET Core 3.1)。Creates a function project that uses the version 3.0 runtime of Azure Functions, which supports .NET Core 3.1. 有关详细信息,请参阅如何指向 Azure Functions 运行时版本For more information, see How to target Azure Functions runtime version.
    模板Template Empty 创建一个空的函数应用。Creates an empty function app.
    存储帐户Storage account 存储模拟器Storage Emulator 要进行持久函数状态管理,需要一个存储帐户。A storage account is required for durable function state management.
  5. 选择“创建” 以创建一个空的函数项目。Select Create to create an empty function project. 此项目具有运行函数所需的基本配置文件。This project has the basic configuration files needed to run your functions.

向应用中添加函数Add functions to the app

以下步骤使用模板在项目中创建持久函数代码。The following steps use a template to create the durable function code in your project.

  1. 在 Visual Studio 中右键单击该项目并选择“添加” > “新建 Azure 函数”。 Right-click the project in Visual Studio and select Add > New Azure Function.

    添加新函数

  2. 确认从“添加”菜单中选择了“Azure 函数”,键入 C# 文件的名称,然后选择“添加”。Verify Azure Function is selected from the add menu, type a name for your C# file, and then select Add.

  3. 选择“Durable Functions 业务流程” 模板并选择“确定”Select the Durable Functions Orchestration template and then select Ok

    选择持久模板

一个新的持久函数将添加到应用中。A new durable function is added to the app. 打开新的 .cs 文件以查看内容。Open the new .cs file to view the contents. 此持久函数是一个简单的函数链接示例,包含以下方法:This durable function is a simple function chaining example with the following methods:

方法Method FunctionNameFunctionName 说明Description
RunOrchestrator <file-name> 管理持久业务流程。Manages the durable orchestration. 在此示例中,业务流程启动,创建一个列表,并将三个函数调用的结果添加到列表中。In this case, the orchestration starts, creates a list, and adds the result of three functions calls to the list. 当三个函数调用完成后,它返回该列表。When the three function calls are complete, it returns the list.
SayHello <file-name>_Hello 此函数返回一个 hello。The function returns a hello. 函数包含要协调的业务逻辑。It is the function that contains the business logic that is being orchestrated.
HttpStart <file-name>_HttpStart HTTP 触发的函数,用于启动业务流程的实例并返回检查状态响应。An HTTP-triggered function that starts an instance of the orchestration and returns a check status response.

现在,你已创建了函数项目和一个持久函数,可以在本地计算机上对其进行测试。Now that you've created your function project and a durable 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 and execute the request. 下面演示浏览器中函数返回的对本地 GET 请求的响应:The following shows the response in the browser to the local GET request returned by the function:

    浏览器中的函数 localhost 响应

    响应是来自 HTTP 函数的初始结果,让我们知道持久业务流程已成功启动。The response is the initial result from the HTTP function letting us know the durable orchestration has started successfully. 它还不是业务流程的最终结果。It is not yet the end result of the orchestration. 响应中包括了几个有用的 URL。The response includes a few useful URLs. 现在,让我们查询业务流程的状态。For now, let's query the status of the orchestration.

  4. 复制 statusQueryGetUri 的 URL 值,将其粘贴到浏览器的地址栏中并执行请求。Copy the URL value for statusQueryGetUri and pasting it in the browser's address bar and execute the request.

    请求将查询业务流程实例的状态。The request will query the orchestration instance for the status. 应当会得到如下所示的最终响应。You should get an eventual response that looks like the following. 此输出显示实例已完成,并且包括了持久函数的输出或结果。This output shows us the instance has completed, and includes the outputs or results of the durable function.

    {
        "instanceId": "d495cb0ac10d4e13b22729c37e335190",
        "runtimeStatus": "Completed",
        "input": null,
        "customStatus": null,
        "output": [
            "Hello Tokyo!",
            "Hello Seattle!",
            "Hello London!"
        ],
        "createdTime": "2019-11-02T07:07:40Z",
        "lastUpdatedTime": "2019-11-02T07:07:52Z"
    }
    
  5. 若要停止调试,请按 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. 在“选取发布目标”**** 中,使用下表中指定的发布选项:In Pick a publish target, use the publish options specified in the following table:

    选项Option 说明Description
    Azure Functions 消耗计划Azure Functions Consumption Plan 在 Azure 云环境中创建一个在消耗计划中运行的函数应用。Create a function app in an Azure cloud environment that runs in a Consumption plan. 使用消耗计划时,只需为函数应用的执行付费。When you use a Consumption plan, you pay only for executions of your functions app. 其他托管计划会产生更高的成本。Other hosting plans incur higher costs. 如果在消耗计划之外的计划中运行,则必须管理函数应用的缩放If you run in a plan other than a Consumption plan, you must manage the scaling of your function app.
    新建Create New 在 Azure 中创建具有相关资源的新函数应用。A new function app, with related resources, is created in Azure.
    如果选择“选择现有”****,则 Azure 中现有函数应用中的所有文件都会被本地项目中的文件覆盖。If you choose Select Existing, all files in the existing function app in Azure are overwritten by files from the local project. 只有在将更新重新发布到现有函数应用时,才使用此选项。Use this option only when you republish 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 deployment, which results in better performance, is the recommended way of running your functions.
    如果不使用此选项,请确保在将函数应用项目发布到 Azure 之前,先停止其在本地的运行。If you don't use this option, make sure to stop your function app project from running locally before you publish to Azure.

    创建发布配置文件

  3. 选择“创建配置文件”****。Select Create Profile. 如果尚未从 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 Azure account.

  4. 在“应用服务:**** 新建”中,使用下表中指定的值:In App Service: Create new, use the values specified in the following table:

    设置Setting ValueValue 说明Description
    名称Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。Name that uniquely identifies your new function app. 接受此名称或输入新名称。Accept this name or enter a new name. 有效的字符是 a-z0-9-Valid characters are: a-z, 0-9, and -.
    订阅Subscription 你的订阅Your subscription 要使用的 Azure 订阅。The Azure subscription to use. 接受此订阅,或从下拉列表中选择一个新订阅。Accept this subscription or select a new one from the drop-down list.
    资源组Resource group 资源组的名称Name of your resource group 要在其中创建函数应用的资源组。The resource group in which to create your function app. 从下拉列表中选择现有资源组,或者选择“新建”来创建新的资源组。****Select an existing resource group from the drop-down list or choose New to create a new resource group.
    托管计划Hosting Plan 托管计划的名称Name of your hosting plan 选择“新建”即可配置一个无服务器计划。****Select New to configure a serverless plan. 确保在“大小”下选择“消耗量”。**** ****Make sure to choose the Consumption under Size. 将项目发布到在消耗计划中运行的函数应用时,只需为函数应用的执行付费。When you publish your project to a function app that runs in a Consumption plan, you pay only for executions of your functions app. 其他托管计划会产生更高的成本。Other hosting plans incur higher costs. 如果在“消耗”之外的计划中运行,则必须管理函数应用的缩放。****If you run in a plan other than Consumption, you must manage the scaling of your function app.
    位置Location 应用服务的位置Location of the app service 在靠近你或者靠近函数访问的其他服务的区域中选择一个位置Choose a Location in a region near you or other services your functions access.
    Azure 存储Azure Storage 常规用途存储帐户General-purpose storage account Functions 运行时需要 Azure 存储帐户。An Azure Storage account is required by the Functions runtime. 选择“新建”即可配置常规用途存储帐户。****Select New to configure a general-purpose storage account. 也可选择一个符合存储帐户要求的现有帐户。You can also choose an existing account that meets the storage account requirements.

    “创建应用服务”对话框

  5. 选择“创建”****,使用这些设置在 Azure 中创建函数应用及其相关的资源,并部署函数项目代码。Select Create to create a function app and its related resources in Azure with these settings and deploy your function project code.

  6. 选择“发布”,然后等待部署完成。Select Publish and wait for the deployment to complete.

    部署完成后,Azure 中函数应用的根 URL 将显示在“发布”选项卡中。After the deployment completes the root URL of the function app in Azure is shown in the Publish tab.

  7. 在“发布”选项卡中,请选择“在 Cloud Explorer 中管理”。In the Publish tab, choose Manage in Cloud Explorer. 此操作会在 Cloud Explorer 中打开新的函数应用 Azure 资源。This opens the new function app Azure resource in Cloud Explorer.

    发布成功消息

    借助 Cloud Explorer,可使用 Visual Studio 来查看网站的内容,启动和停止函数应用,并直接浏览到 Azure 上和 Azure 门户中的函数应用资源。Cloud Explorer lets you use Visual Studio to view the contents of the site, start and stop the function app, and browse directly to function app resources on Azure and in the Azure portal.

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

    调用持久函数 HTTP 触发器的 URL 应采用以下格式:The URL that calls your durable function HTTP trigger should be in the following format:

    https://<APP_NAME>.chinacloudsites.cn/api/<FUNCTION_NAME>_HttpStart

  2. 将 HTTP 请求的这个新 URL 粘贴到浏览器的地址栏中。Paste this new URL for the HTTP request into your browser's address bar. 你应当会得到与之前使用已发布的应用时相同的状态响应。You should get the same status response as before when using the published app.

后续步骤Next steps

你已使用 Visual Studio 创建并发布了一个 C# 持久函数应用。You have used Visual Studio to create and publish a C# durable function app.