使用 Visual Studio Code 开发 C# Service Fabric 应用程序Develop C# Service Fabric applications with Visual Studio Code

使用适用于 VS Code 的 Service Fabric Reliable Services 扩展可在 Windows、Linux 和 macOS 操作系统上轻松生成 .NET Core Service Fabric 应用程序。The Service Fabric Reliable Services extension for VS Code makes it easy to build .NET Core Service Fabric applications on Windows, Linux, and macOS operating systems.

本文介绍如何使用 Visual Studio Code 生成、部署和调试 .NET Core Service Fabric 应用程序。This article shows you how to build, deploy, and debug a .NET Core Service Fabric application using Visual Studio Code.

先决条件Prerequisites

本文假设你已安装 VS Code、适用于 VS Code 的 Service Fabric Reliable Services 扩展,以及开发环境所需的所有依赖项。This article assumes that you have already installed VS Code, the Service Fabric Reliable Services extension for VS Code, and any dependencies required for your development environment. 有关详细信息,请参阅入门To learn more, see Getting Started.

下载示例Download the sample

本文使用 Service Fabric .NET Core 入门示例 GitHub 存储库中的 CounterService 应用程序。This article uses the CounterService application in the Service Fabric .NET Core getting started samples GitHub repository.

若要将此存储库克隆到开发计算机,请在终端窗口(Windows 上的命令窗口)中运行以下命令:To clone the repository to your development machine, run the following command from a terminal window (command window on Windows):

git clone https://github.com/Azure-Samples/service-fabric-dotnet-core-getting-started.git

在 VS Code 中打开应用程序Open the application in VS Code

WindowsWindows

右键单击“开始”菜单中的 VS Code 图标,并选择“以管理员身份运行” 。Right-click the VS Code icon in the Start Menu and choose Run as administrator. 若要将调试程序附加到服务,需要以管理员身份运行 VS Code。To attach the debugger to your services, you need to run VS Code as administrator.

LinuxLinux

使用终端导航到应用程序本地克隆到的目录中的 /service-fabric-dotnet-core-getting-started/Services/CounterService 路径。Using the terminal, navigate to the path /service-fabric-dotnet-core-getting-started/Services/CounterService from the directory that the application was cloned into locally.

运行以下命令,以 root 用户身份打开 VS Code,以便将调试程序附加到服务。Run the following command to open VS Code as a root user so that the debugger can attach to your services.

sudo code . --user-data-dir='.'

现在,该应用程序应会显示在 VS Code 工作区中。The application should now appear in your VS Code workspace.

工作区中的计数器服务应用程序

构建应用程序Build the application

  1. 在 VS Code 中,按 (Ctrl + Shift + p) 打开 命令面板Press (Ctrl + Shift + p) to open the Command Palette in VS Code.

  2. 搜索并选择“Service Fabric: 生成应用程序”命令。Search for and select the Service Fabric: Build Application command. 生成输出将发送到集成式终端。The build output is sent to the integrated terminal.

    VS Code 中的“生成应用程序”命令

将应用程序部署到本地群集Deploy the application to the local cluster

生成应用程序后,可将其部署到本地群集。After you have built the application, you can deploy it to the local cluster.

  1. 命令面板 中,选择“Service Fabric: 部署应用程序(Localhost)”命令。From the Command Palette, select the Service Fabric: Deploy Application (Localhost) command. 安装过程的输出将发送到集成式终端。The output of the install process is sent to the integrated terminal.

    VS Code 中的“部署应用程序”命令

  2. 部署完成后,启动浏览器并打开 Service Fabric Explorer:http://localhost:19080/Explorer。When the deployment is complete, launch a browser and open Service Fabric Explorer: http://localhost:19080/Explorer. 应会看到应用程序正在运行。You should see that the application is running. 此过程可能需要一段时间,请耐心等待。This may take some time, so be patient.

    Service Fabric Explorer 中的计数器服务应用程序

  3. 确认应用程序正在运行后,启动浏览器并打开此页:http://localhost:31002。After you've verified the application is running, launch a browser and open this page: http://localhost:31002. 这是应用程序的 Web 前端。This is the web front-end of the application. 刷新页面,查看递增的当前计数器值。Refresh the page to see the current value of the counter as it increments.

    浏览器中的计数器服务应用程序

将应用程序发布到 Azure Service Fabric 群集Publish the application to an Azure Service Fabric cluster

除了将应用程序部署到本地群集外,还可以将应用程序发布到远程 Azure Service Fabric 群集。Along with deploying the application to the local cluster, you can also publish the application to a remote Azure Service Fabric cluster.

  1. 确保已按上面的说明生成了应用程序。Ensure that you have built your application using the instructions above. 使用要发布到的远程群集的详细信息更新生成的配置文件 Cloud.jsonUpdate the generated configuration file Cloud.json with the details of the remote cluster you want to publish to.

  2. 命令面板 中,选择“Service Fabric: 发布应用程序”命令。From the Command Palette, select the Service Fabric: Publish Application command. 安装过程的输出将发送到集成式终端。The output of the install process is sent to the integrated terminal.

    VS Code 中的“发布应用程序”命令

  3. 部署完成后,启动浏览器并打开 Service Fabric Explorer: https:<clusterurl>:19080/ExplorerWhen the deployment is complete, launch a browser and open Service Fabric Explorer: https:<clusterurl>:19080/Explorer. 应会看到应用程序正在运行。You should see that the application is running. 此过程可能需要一段时间,请耐心等待。This may take some time, so be patient.

调试应用程序Debug the application

在 VS Code 中调试应用程序时,该应用程序必须在本地群集上运行。When debugging applications in VS Code, the application must be running on a local cluster. 然后,可将断点添加到代码中。Breakpoints can then be added to the code.

若要设置断点并调试,请完成以下步骤:To set a breakpoint and debug, complete the following steps:

  1. 在 Explorer 中打开 /src/CounterServiceApplication/CounterService/CounterService.cs 文件,并在 RunAsync 方法中的第 62 行处设置一个断点。In Explorer, open the /src/CounterServiceApplication/CounterService/CounterService.cs file and set a breakpoint at line 62 inside the RunAsync method.

  2. 单击 活动栏 中的“调试”图标,在 VS Code 中打开调试程序视图。Click the Debug icon in the Activity Bar to open the debugger view in VS Code. 单击调试程序视图顶部的齿轮图标,从环境下拉菜单中选择“.NET Core”。 Click the gear icon at the top of the debugger view and select .NET Core from the dropdown environment menu. 此时会打开 launch.json 文件。The launch.json file opens. 可以关闭此文件。You can close this file. 现在,运行按钮(绿色箭头)旁边的调试配置菜单中应会出现配置选项。Now you should see configuration choices in the debug configuration menu located next to the run button (green arrow).

    VS Code 工作区中的调试图标

  3. 在调试配置菜单中选择“.NET Core 附加”。 Select .NET Core Attach from the debug configuration menu.

    屏幕截图显示“调试配置”菜单中处于选定状态的“.NET Core 附加”。

  4. 在浏览器中打开 Service Fabric Explorer:http://localhost:19080/Explorer。Open Service Fabric Explorer in a browser: http://localhost:19080/Explorer. 单击“应用程序”并向下钻取,以确定运行 CounterService 的主节点。 Click Applications and drill down to determine the primary node that the CounterService is running on. 在下图中,CounterService 的主节点是“节点 0”。In the image below the primary node for the CounterService is Node 0.

    CounterService 的主节点

  5. 在 VS Code 中,单击“.NET Core 附加”调试配置旁边的运行图标(绿色箭头)。 In VS Code, click the run icon (green arrow) beside the .NET Core Attach debug configuration. 在进程选择对话框中,选择在步骤 4 中识别的主节点上运行的 CounterService 进程。In the process selection dialog, select the CounterService process that is running on the primary node that you identified in step 4.

    主进程

  6. 很快就会命中 CounterService.cs 文件中的断点。The breakpoint in the CounterService.cs file will be hit very quickly. 然后可以浏览本地变量的值。You can then explore the values of the local variables. 使用 VS Code 顶部的“调试”工具栏继续执行、逐行执行每行、逐步执行方法,或跳出当前方法。Use the Debug toolbar at the top of VS Code to continue execution, step over lines, step into methods, or step out of the current method.

    调试值

  7. 若要结束调试会话,请单击 VS Code 顶部的“调试”工具栏上的插入图标。To end the debugging session, click the plug icon on the Debug toolbar at the top of VS Code..

    从调试程序断开连接

  8. 完成调试后,可以使用“Service Fabric: 删除应用程序”命令从本地群集中删除 CounterService 应用程序。When you've finished debugging, you can use the Service Fabric: Remove Application command to remove the CounterService application from your local cluster.

后续步骤Next steps