教程:Azure Functions 入门Tutorial: Getting started with Azure Functions

在本实验室中,了解如何使用 Visual Studio for Mac 开始生成 Azure Functions。In this lab, you'll learn how to get started building Azure Functions using Visual Studio for Mac. 还将与 Azure 存储表集成,该存储表表示可供 Azure Functions 开发人员使用的众多绑定和触发器之一。You'll also integrate with Azure storage tables, which represent one of the many kinds of bindings and triggers available to Azure Functions developers.

目标Objectives

  • 创建和调试本地 Azure FunctionsCreate and debug local Azure Functions
  • 与 Web 和 Azure 存储资源集成Integrate with web and Azure storage resources
  • 安排涉及多个 Azure Functions 的工作流Orchestrate a workflow involving multiple Azure Functions

要求Requirements

练习 1:创建 Azure Functions 项目Exercise 1: Creating an Azure Functions project

  1. 启动“Visual Studio for Mac” 。Launch Visual Studio for Mac.

  2. 选择“文件”>“新建解决方案” 。Select File > New Solution.

  3. 从“云”>“常规” 类别中,选择“Azure Functions” 模板。From the Cloud > General category, select the Azure Functions template. 使用 C# 创建托管 Azure Functions 的 .NET 类库。You will use C# to create a .NET class library that hosts Azure Functions. 单击 “下一步”Click Next.

    Azure Functions 模板选择

  4. 将“项目名称” 设置为“AzureFunctionsLab” ,单击“创建” 。Set the Project Name to "AzureFunctionsLab" and click Create.

    命名和创建 Azure Function 项目

  5. 展开“Solution Pad” 中的节点。Expand the nodes in Solution Pad. 默认项目模板包括对各种 Azure WebJobs 包以及 Newtonsoft.Json 包的 NuGet 引用。The default project template includes NuGet references to a variety of Azure WebJobs packages, as well as the Newtonsoft.Json package.

    此外还有三个文件:- 用于描述主机的全局配置选项的 host.json 用于配置服务设置的 local.settings.json 。There are also three files: - host.json for describing the global configuration options for the host - local.settings.json for configuring service settings. - 项目模板还会创建一个默认 HttpTrigger。- The project template also creates a default HttpTrigger. 对于本实验室,应从项目中删除 HttpTrigger.cs 文件。For the sake of this lab, you should delete the HttpTrigger.cs file from the project.

    打开 local.settings.json 。Open local.settings.json. 它默认具有两个空的连接字符串设置。It defaults to having two empty connection string settings.

    显示 local.settings.json 文件的 solution pad

练习 2:创建 Azure 存储帐户Exercise 2: Creating an Azure storage account

  1. https://portal.azure.com 登录到 Azure 帐户。Log on to your Azure account at https://portal.azure.com.

  2. 在屏幕左侧的“收藏夹” 部分下,选择“存储帐户” :Under the Favorites section, located on the left of the screen, select Storage Accounts:

    Azure 门户中显示存储帐户项的收藏夹部分

  3. 选择“添加” ,创建新的存储帐户:Select Add to create a new storage account:

    添加新存储帐户的按钮

  4. 在“名称” 中输入全局唯一名称,然后在“资源组” 中再次使用该名称。Enter a globally unique name for the Name and reuse it for the Resource group. 可将其他所有项保留为其默认值。You can keep all the other items as their default.

    新存储帐户的详细信息

  5. 单击 “创建”Click Create. 创建存储帐户可能需要几分钟的时间。It might take a few minutes to create the storage account. 成功创建后,你将收到一条通知。You'll get a notification once it has been successfully created.

    部署成功通知

  6. 选择通知中的“转到资源” 按钮。Select the Go to resource button from the notification.

  7. 选择“访问密钥” 选项卡。Select the Access keys tab.

    访问密钥设置

  8. 复制第一个连接字符串 。Copy the first Connection String. 之后使用此字符串将 Azure 存储与 Azure Functions 集成。This string is used to integrate Azure storage with your Azure Functions later on.

    密钥 1 的信息

  9. 返回 Visual Studio for Mac ,将完整的连接字符串粘贴在 local.settings.json 中作为 AzureWebJobsStorage 设置。Return to Visual Studio for Mac and paste the full connection string in as the AzureWebJobsStorage setting in local.settings.json. 现在便可在需访问其资源的函数的特性中引用该设置的名称。Now you can reference the name of the setting in attributes for functions that need access to its resources.

    含有输入的连接密钥的本地设置文件

示例 3:创建和调试 Azure 函数Example 3: Creating and debugging an Azure Function

  1. 现在可以开始添加一些代码。You're now ready to start adding some code. 使用 .NET 类库时,Azure Functions 将作为静态方法进行添加。When working with a .NET class library, Azure Functions are added as static methods. 在“Solution Pad”中,右键单击“AzureFunctions”项目节点,选择“添加”>“添加函数” :From Solution Pad, right-click the AzureFunctions project node and select Add > Add Function:

    “添加函数”选项

  2. 在“新建 Azure Functions”对话框中,选择常规 Webhook 模板。In the New Azure Functions dialog, select the Generic webhook template. 将“名称” 设置为“Add” ,单击“确定” 创建函数:Set the Name to Add and click Ok to create your function:

    “新建 Azure Functions”对话框

  3. 在新文件的顶部,添加以下 using 指令:At the top of the new file, add the using directives below:

    using Microsoft.Azure.WebJobs.Extensions.Http;
    using System.Web;
    using Microsoft.WindowsAzure.Storage.Table;
    
  4. 删除现有 Run 方法,并将以下方法添加到类中作为 Azure Function:Remove the existing Run method and add the method below to the class as your Azure Function:

    [FunctionName("Add")]
    public static int Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]
    HttpRequestMessage req,
    TraceWriter log)
    {
        int x = 1;
        int y = 2;
    
        return x + y;
    }
    
  5. 让我们逐部分地了解此方法。Let's walk through the method definition piece by piece.

    首先看到的是将此方法标记为 Azure Function 的 FunctionName 特性。The first thing you'll see is the FunctionName attribute that marks this method as an Azure Function. 该特性指定函数的公共名称。The attribute designates the public name of the function. 特性名称无需与实际方法名称匹配。The attribute name doesn't need to match the actual method name.

    FunctionName 特性突出显示的新 Run 方法

  6. 接下来,需将该方法标记为“公共静态” 方法。Next, the method is marked as a public static method, which is required. 你还会发现,返回值为 int 。除非另行指定 using 方法特性,否则 Azure Function 的任何非无效返回值将以文本形式返回到客户端。You'll also notice that the return value is an int. Unless otherwise specified using method attributes, any non-void return value of an Azure Function is returned to the client as text. 默认情况下,它以 XML 形式返回,但可将其更改为 JSON (之后将在本实验室中执行此操作)。By default it's returned as XML, but can be changed to JSON, which you'll do later on in the lab.

    方法初始化突出显示的新 Run 方法

  7. 第一个参数将使用 HttpTrigger 特性进行标记,该特性表示此方法由 HTTP 请求调用。The first parameter is marked with the HttpTrigger attribute, which indicates that this method is invoked by an HTTP request. 该特性还指定该方法的授权级别,以及它支持的谓词(此情况下只有“GET” )。The attribute also specifies the authorization level of the method, as well as the verbs it supports (only "GET" in this case). 也可以选择定义一个“Route” 来替代该方法的路径,并提供一个方法以自动从路径中提取变量。You may also optionally define a Route that overrides the path to the method and offers a way to automatically extract variables from the path. 由于此处“Route” 为 null,所以此方法的路径将默认为 /api/Add 。Since the Route is null here, the path to this method will default to /api/Add.

    参数突出显示的新 Run 方法

  8. 该方法的最后一个参数是 TraceWriter ,可使用它记录诊断和错误的消息。The final parameter to the method is a TraceWriter that can be used to log messages for diagnostics and errors.

    TraceWriter 突出显示的新 Run 方法

  9. 单击该方法的 return 行的边距,在该行设置一个断点 :Set a breakpoint on the return line of the method by clicking in the margin of the line:

    已在 return 行设置断点

  10. 按 F5 或选择“运行”>“启动调试” ,生成并运行调试会话中的项目。Build and run the project in a debug session by pressing F5 or selecting Run > Start Debugging. 或者,可以单击“运行” 按钮。You could alternatively click the Run button. 所有这些选项都执行相同任务。These options all perform the same task. 此实验室的其余部分引用 F5 ,但可以使用你喜欢的方法。The rest of this lab references F5, but you can use the method you find most comfortable.

    生成并运行项目

  11. 运行项目将自动打开终端应用程序。Running the project will automatically open the Terminal application.

  12. 该项目将根据方法特性和本文稍后将介绍的文件约定经历整个检测 Azure Functions 流程。The project goes through a process of detecting Azure Functions based on method attributes and a file convention that is covered later in this article. 在这种情况下,它会检测到一个 Azure Function 并“生成”1 个作业函数。In this case, it detects a single Azure Function and "generates" 1 job function.

    终端中 Azure Functions 的输出

  13. 在启动消息的底部,Azure Functions 主机将打印任何 HTTP 触发器 API 的 URL。At the bottom of the startup messages, the Azure Functions host prints the URLs of any HTTP trigger APIs. 应该只有一个。There should only be one. 复制该 URL 并将其粘贴在新的浏览器选项卡中。Copy that URL and paste it in a new browser tab.

    Azure Function API URL

  14. 应立即触发该断点。The breakpoint should trigger immediately. 已将 Web 请求路由到该函数,现在可以调试该请求。The web request has been routed to the function and can now be debugged. 将鼠标移动到 x 变量上,查看其值。Mouse over the x variable to see its value.

    已触发断点

  15. 可采用之前添加该断点的方法来删除它(单击边距或选择该行,然后按 F9 )。Remove the breakpoint using the same method used to add it earlier (click on the margin or select the line and press F9).

  16. 按 F5 继续运行 。Press F5 to continue running.

  17. 浏览器中将呈现该方法的 XML 结果。In the browser, the XML result of the method will be rendered. 正如预期一样,硬编码加法运算生成了一个合理的总和。As expected, the hardcoded addition operation produces a plausible sum. 注意,如果只在 Safari 中看到“3”,请转到“Safari”>“首选项”>“高级” ,勾选“在菜单栏中显示‘开发’菜单” 复选框,再重新加载页面。Note, if you only see "3" in Safari, go to Safari > Preferences > Advanced and tick the "Show Develop menu in menu bar" checkbox and reload the page.

  18. 在“Visual Studio for Mac”中 ,单击“停止” 按钮,结束调试会话。In Visual Studio for Mac, click the Stop button to end the debug session. 若要确保获得新的更改,请记住重启(先停止,然后运行)调试会话。To ensure that new changes are picked up, don't forget to restart (stop and then run) the debugging session.

    “停止调试”选项

  19. 在 Run 方法中,将 x 和 y 定义替换替换为以下代码。In the Run method, replace the x and y definitions with the code below. 此代码从 URL 的查询字符串中提取值,以便可以根据提供的参数动态执行添加操作。This code extracts values from the URL's query string so that the addition operation can be performed dynamically based on the provided parameters.

    var query = HttpUtility.ParseQueryString(req.RequestUri.Query);
    
    int x = int.Parse(query["x"]);
    
    int y = int.Parse(query["y"]);
    
    return x + y;
    
  20. 运行该应用程序。Run the application.

  21. 返回到浏览器窗口,将字符串 /?x=2&y=3 追加到 URL。Return to the browser window and append the string /?x=2&y=3 to the URL. 整个 URL 现在应为 http://localhost:7071/api/Add?x=2&y=3The whole URL should now be http://localhost:7071/api/Add?x=2&y=3. 导航到新的 URL。Navigate to the new URL.

  22. 此时的结果应反映新的参数。This time the result should reflect the new parameters. 可使用不同的值运行该项目。Feel free to run the project with different values. 注意,由于未进行任何错误检查,因此无效的或缺少的参数将引发错误。Note that there isn't any error checking, so invalid or missing parameters will throw an error.

  23. 停止调试会话。Stop the debugging session.

练习 4:使用 function.jsonExercise 4: Working with function.json

  1. 在之前的练习中,提到了 Visual Studio for Mac 为库中定义的 Azure Function“生成了”一个作业函数。In an earlier exercise, it was mentioned that Visual Studio for Mac "generated" a job function for the Azure Function defined in the library. 这是因为 Azure Functions 实际上并未在运行时使用该方法特性,而使用的是编译时文件系统约定来配置 Azure Functions 可供使用的位置及方式。This is because Azure Functions doesn't actually use the method attributes at runtime, but rather uses a compile-time file system convention to configure where and how Azure Functions are made available. 在“Solution Pad” 中,右键单击项目节点并选择“在查找器中展现” 。From Solution Pad, right-click on your project node and select Reveal in Finder.

    “在查找器中展现”菜单选项

  2. 向下导航文件系统,直至到达 bin/Debug/netstandard2.0 。Navigate down the file system until you reach bin/Debug/netstandard2.0. 此处应该有一个名为“Add” 的文件夹。There should be a folder named Add. 创建此文件夹以与 C# 代码中的函数名称特性对应。This folder was created to correspond with the function name attribute in the C# code. 展开 Add 文件夹,会显示一个 function.json 文件。Expand the Add folder to reveal a single function.json file. 此文件由运行时使用,用来托管和管理该 Azure Function。This file is used by the runtime to host and manage the Azure Function. 对于其他不支持编译时的语言模型(如 C# 脚本或 JavaScript),必须手动创建和维护这些文件夹。For other language models without compile-time support (such as C# script or JavaScript), these folders must be manually created and maintained. 对于 C# 开发人员,这些文件夹会在构建时自动从特性元数据生成。For C# developers, they are automatically generated from attribute metadata upon build. 右键单击 function.json ,选择在 Visual Studio 中打开它。Right-click on function.json and select to open it in Visual Studio.

    文件目录中的 function.json

  3. 基于本教程的前几个步骤,你应该对 C# 特性已经有了基本的了解。Given the previous steps of this tutorial, you should have a basic understanding of C# attributes. 鉴于此,此 JSON 应该看起来很熟悉。Taking that into account, this JSON should look familiar. 但是,还有几个之前的练习中未介绍的项目。However, there are a few items that were not covered in earlier exercises. 例如,每个 binding 必须设置其 direction 。For example, each binding must have its direction set. 正如你所推测的一样,“in” 指示输入参数,而“out” 指示参数是一个返回值(通过 $return )或者是该方法的“out” 参数。As you might infer, "in" means that the parameter is input, whereas "out" indicates that the parameter is either a return value (via $return) or an out parameter to the method. 此外,还需在程序集内指定 scriptFile (相对于此最终位置)和 entryPoint 方法(公共和静态)。You also need to specify the scriptFile (relative to this final location) and the entryPoint method (public and static) within the assembly. 在接下来的几个步骤中,使用此模型添加一个自定义函数路径,以便将此文件的内容复制到剪贴板。In the next few steps you'll add a custom function path using this model, so copy the contents of this file to the clipboard.

    visual studio for mac 中打开的 function.json 文件

  4. 在“Solution Pad” 中,右键单击 AzureFunctionsLab 项目节点,选择“添加”>“新建文件夹” 。In Solution Pad, right-click the AzureFunctionsLab project node and select Add > New Folder. 将新文件夹命名为 Adder 。Name the new folder Adder. 按照默认约定,此文件夹的名称将定义 API 的路径,例如 api/Adder 。By default convention, the name of this folder will define the path to the API, such as api/Adder.

    “新建文件夹”选项

  5. 右键单击 Adder 文件夹,选择“添加”>“新建文件” 。Right-click the Adder folder and select Add > New File.

    “新建文件”选项

  6. 选择“Web” 类别和“空 JSON 文件” 模板。Select the Web category and the Empty JSON File template. 将“名称” 设置为“function” ,单击“新建” 。Set the Name to function and click New.

    “空 json 文件”选项

  7. 粘贴另一个 function.json (步骤 3 中)的内容来替代新创建的文件的默认内容。Paste the contents of the other function.json (from step 3) in to replace the default contents of the newly created file.

  8. 从 json 文件的顶部删除以下行:Remove the following lines from the top of the json file:

    "configurationSource":"attributes",
    "generatedBy":"Microsoft.NET.Sdk.Functions-1.0.13",
    
  9. 在第一个绑定的末尾("name": "req" 行之后),添加以下属性。At the end of the first binding (after the "name": "req" line), add the properties below. 请记住在上一行中加上一个逗号。Don't forget to include a comma on the previous line. 此属性将替代默认根,这样它现在便可从该路径提取 int 参数,并将它们置于名为 x 和 y 的方法参数中。This property overrides the default root such that it will now extract int parameters from the path and place them into method parameters that are named x and y.

    "direction": "in",
    "route": "Adder/{x:int?}/{y:int?}"
    
  10. 在第一个绑定下添加另一个绑定。Add another binding underneath the first. 此绑定处理函数的返回值。This binding handles the return value of the function. 请记住在上一行中加上一个逗号:Don't forget to include a comma on the previous line:

    {
    "name": "$return",
    "type": "http",
    "direction": "out"
    }
    
  11. 此外,更新文件底部的 entryPoint 属性,以便使用名为“Add2” 的方法,如下所示。Also update the entryPoint property at the bottom of the file to use a method called "Add2", such as shown below. 这是为了说明可将路径 api/Adder... 映射至具有任意名称(此处为“Add2” )的适当方法。This is to illustrate that the path api/Adder... could map to an appropriate method with any name (Add2 here).

    "entryPoint": "<project-name>.<function-class-name>.Add2"
    
  12. 最终的 function.json 文件应如以下 json 所示:Your final function.json file should look like the following json:

    {
    "bindings": [
        {
        "type": "httpTrigger",
        "methods": [
            "get"
        ],
        "authLevel": "function",
        "direction": "in",
        "name": "req",
        "route": "Adder/{x:int?}/{y:int?}"
        },
        {
        "name": "$return",
        "type": "http",
        "direction": "out"
        }
    ],
    "disabled": false,
    "scriptFile": "../bin/AzureFunctionsProject.dll",
    "entryPoint": "AzureFunctionsProject.Add.Add2"
    }
    
  13. 要使上述所有操作均发挥作用,所需的最后一步是指示 Visual Studio for Mac 在此文件每次更改时都将其复制到输出目录中的相同相对路径中。The one final step required to make this all work is to instruct Visual Studio for Mac to copy this file to the same relative path in the output directory every time it changes. 选中该文件,从右侧菜单栏中选择“属性”选项卡,然后在“复制到输出目录” 中,选择“如果较新则复制” :With the file selected, choose the properties tab from the right-hand bar, and for Copy to output directory select Copy if newer:

    Json 文件的“属性”选项

  14. 在 Add.cs 中 ,将 Run 方法(包括特性)替换为以下方法,实现预期的功能。In Add.cs, replace the Run method (including the attribute) with the following method to fulfill the expected function. 它与 Run 非常类似,只不过它不使用特性,并且对于 x 和 y 具有显式参数。It's very similar to Run, except that it uses no attributes and has explicit parameters for x and y.

    public static int Add2(
        HttpRequestMessage req,
        int x,
        int y,
        TraceWriter log)
    {
        return x + y;
    }
    
  15. 按 F5 生成并运行项目。Press F5 to build and run the project.

  16. 在构建完成并且平台运行时,它将指示为映射至新添加方法的请求提供了第二个路由:As the build completes and platform spins up, it will now indicate that there is a second route available for requests that maps to the newly added method:

    Http 函数的 URL

  17. 返回浏览器窗口并导航到 http://localhost:7071/api/Adder/3/5 。Return the browser window and navigate to http://localhost:7071/api/Adder/3/5.

  18. 此时,该方法将再次运行,从该路径中拉取参数并生成一个总和。This time the method works once again, pulling parameters from the path and producing a sum.

  19. 返回到 Visual Studio for Mac ,并结束调试会话。Return to Visual Studio for Mac and end the debugging session.

练习 5:使用 Azure 存储表Exercise 5: Working with Azure storage tables

通常,你生成的服务可能比我们目前为止已生成的服务更复杂,并且需要占用大量的时间和/或基础结构来执行。Often, the service you build might be much more complex than what we have built so far and require a significant amount of time and/or infrastructure to execute. 在这种情况下,你可能会发现在 Azure Functions 提供支持的资源可用时,它在接受已排队等待处理的请求方面很有效。In that case, you might find it effective to accept requests that are queued for processing when the resources become available, which Azure Functions provides support for. 在其他情况下,需集中存储数据。In other cases, you’ll want to store data centrally. 通过 Azure 存储表,可以快速做到这一点。Azure Storage tables let you do that quickly.

  1. 将以下类添加到 Add.cs 。Add the class below to Add.cs. 它应该位于命名空间内、现有类外。It should go inside the namespace, but outside the existing class.

    public class TableRow : TableEntity
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Sum { get; set; }
    }
    
  2. 在 Add 类中,添加以下代码以引入另一个函数。Within the Add class, add the code below to introduce another function. 注意,到目前为止,它是唯一的,因为它不涉及 HTTP 响应。Note that this one is unique so far in that it doesn't involve an HTTP response. 最后一行返回了新的 TableRow ,其中填有可使其之后易于检索的一些关键信息(PartitionKey 和 RowKey ),还返回了其参数和总和。The final line returns a new TableRow populated with some key information that will make it easy to retrieve later on (PartitionKey and RowKey), as well as its parameters and sum. 该方法内的代码还使用 TraceWriter ,以便更轻松地了解函数何时运行。The code within the method also uses the TraceWriter to make it easier to know when the function runs.

    [FunctionName("Process")]
    [return: Table("Results")]
    public static TableRow Process(
        [HttpTrigger(AuthorizationLevel.Function, "get",
            Route = "Process/{x:int}/{y:int}")]
        HttpRequestMessage req,
        int x,
        int y,
        TraceWriter log)
    {
        log.Info($"Processing {x} + {y}");
    
        return new TableRow()
        {
            PartitionKey = "sums",
            RowKey = $"{x}_{y}",
            X = x,
            Y = y,
            Sum = x + y
        };
    }
    
  3. 按 F5 生成并运行项目。Press F5 to build and run the project.

  4. 在浏览器选项卡中,导航到 http://localhost:7071/api/Process/4/6 。In the browser tab, navigate to http://localhost:7071/api/Process/4/6. 这会将另一条消息置于队列中,最终将使另一行添加到表中。This will put another message into the queue, which should eventually result in another row being added to the table.

  5. 返回到“终端” ,监视对“4 + 6” 的传入请求。Return to Terminal and watch for the incoming request for 4 + 6.

    显示添加请求的终端输出

  6. 返回到浏览器,刷新对相同 URL 的请求。Return to the browser to refresh the request to the same URL. 此时,将在 Process 方法后看到一个错误。This time you'll see an error after the Process method. 这是因为该代码尝试使用一个已存在的分区键和行键组合,向 Azure 表存储表添加一行。This is because the code is attempting to add a row to the Azure Table Storage table using a partition and row key combination that already exists.

    System.Private.CoreLib: Exception while executing function: Process. Microsoft.Azure.WebJobs.Host: Error while handling parameter $return after function returned:. Microsoft.Azure.WebJobs.Host: The specified entity already exists.
    
  7. 结束调试会话。End the debugging session.

  8. 若要解决错误,立即将以下参数添加到 TraceWriter 参数前的方法定义。To mitigate the error, add the following parameter to the method definition immediately before the TraceWriter parameter. 此参数指示 Azure Functions 平台尝试从 PartitionKey 上的“Results” 表检索 TableRow (我们一直将其用于存储结果)。This parameter instructs the Azure Functions platform to attempt to retrieve a TableRow from the Results table on the PartitionKey we've been using to store results. 但是,一个突出的优点是,基于其他 xy 参数,会为相同方法动态生成 RowKeyHowever, some of the real magic comes into play when you notice that the RowKey is being dynamically generated based on the other x and y parameters for the very same method. 如果该行已存在,则在该方法开始时,tableRow 即拥有此行,无需开发人员执行额外的工作。If that row already exists, then tableRow will have it when the method begins with no extra work required by the developer. 如果该行不存在,则为 null。If the row doesn't exist, then it'll just be null. 这种高效使开发人员能够专注于重要的业务逻辑而非基础结构。This sort of efficiency enables developers to focus on the important business logic and not the infrastructure.

    [Table("Results", "sums", "{x}_{y}")]
    TableRow tableRow,
    
  9. 将以下代码添加到方法的开头。Add the code below to the beginning of the method. 如果 tableRow 不为 null,则我们已有要请求的操作的结果,并且可立即返回它。If tableRow isn't null, then we already have the results for the operation being requested and can return it immediately. 否则,该函数将像以前那样继续运行。Otherwise, the function continues as before. 虽然这可能不是返回数据的最可靠方式,但它表明可通过很少的代码跨多个可伸缩层安排非常复杂的操作。While this may not be the most robust way to return the data, it illustrates the point that you can orchestrate incredibly sophisticated operations across multiple scalable tiers with very little code.

    if (tableRow != null)
    {
        log.Info($"{x} + {y} already exists");
        return null;
    }
    
  10. 按 F5 生成并运行项目。Press F5 to build and run the project.

  11. 在浏览器选项卡中,刷新 http://localhost:7071/api/Process/4/6 处的 URL。In the browser tab, refresh the URL at http://localhost:7071/api/Process/4/6. 由于此记录的表行已存在,因此它应立即返回且不能出错。Since the table row for this record exists, it should return immediately and without error. 由于不存在任何 HTTP 输出,因此可在终端中查看输出。Since there is no HTTP output, you can see the output in Terminal.

    显示表行已存在的终端输出

  12. 更新该 URL 反映来尚未测试的组合,如 http://localhost:7071/api/Process/5/7 。Update the URL to reflect a combination not yet tested, such as http://localhost:7071/api/Process/5/7. 注意终端中的消息,它指示未(如预期)找到该表行。Note the message in Terminal, which indicates that the table row was not found (as expected).

    显示新进程的终端输出

  13. 返回到 Visual Studio for Mac ,并结束调试会话。Return to Visual Studio for Mac and end the debugging session.

总结Summary

在本实验室中,你已了解如何使用 Visual Studio for Mac 开始生成 Azure Functions。In this lab, you've learned how to get started building Azure Functions with Visual Studio for Mac.