我是靠谱客的博主 激情小猫咪,最近开发中收集的这篇文章主要介绍Dapr + .NET 实战(九)本地调试,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前几节开发Dapr应用程序时,我们使用 dapr cli 来启动dapr服务,就像这样:

dapr run --dapr-http-port 3501 --app-port 5001  --app-id frontend dotnet  .FrontEndbinDebugnet5.0FrontEnd.dll

如果你想要通过dapr调试服务呢?在这里使用 dapr 运行时(daprd) 来帮助实现这一点。具体原理就是先从命令行中运行符合正确参数的 daprd,然后启动您的代码并附加调试器。

1.配置launch.json

vscode打开项目,并创建launch.json

bd7ea494d9908fc3062cba813bc37745.png

 修改launch.json的preLaunchTask,自定义名字,preLaunchTask将引用在 tasks.json 文件中定义的任务。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Frontend-.NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            //"preLaunchTask": "build",
            "preLaunchTask": "daprd-frontend",
            "program": "${workspaceFolder}/FrontEnd/bin/Debug/net5.0/FrontEnd.dll",
            "args": [],
            "cwd": "${workspaceFolder}/FrontEnd",
            "stopAtEntry": false,
            "serverReadyAction": {
                "action": "openExternally",
                "pattern": "\bNow listening on:\s+(https?://\S+)"
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceFolder}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
2.配置task.json

需要在task.json文件中定义一个 daprd task和问题匹配器(problem matcher)。这里有两个通过上述 preLaunchTask 成员引用。在 dpred -frontend task下,还有一个dependsOn成员,它引用build任务,以确保最新的代码正在运行/调试。用了 problemMatcher,这样当 daprd 进程启动和运行时,VSCode 就能够知道。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/FrontEnd/FrontEnd.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/FrontEnd/FrontEnd.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "${workspaceFolder}/FrontEnd/FrontEnd.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "daprd-frontend",
            "command": "daprd",
            "args": [
                "-app-id",
                "frontend",
                "-app-port",
                "5001",
                "-dapr-http-port",
                "3501",
                "-placement-host-address",
                "localhost:6050",
                "-components-path",
                "C:\Users\chesterychen\.dapr\components"
            ],
            "isBackground": true,
            "problemMatcher": {
                "pattern": [
                    {
                      "regexp": ".",
                      "file": 1,
                      "location": 2,
                      "message": 3
                    }
                ],
                "background": {
                    "beginsPattern": "^.*starting Dapr Runtime.*",
                    "endsPattern": "^.*waiting on port.*"
                }
            },
            "dependsOn": "build"
        },
    ]
}

因为没有使用 dapr run* cli 命令, 所以运行 daprd list 命令将不会显示当前正在运行的应用列表。

3.调试

在StateController.GetAsync中新增断点,运行并调用http://192.168.43.94:3501/v1.0/invoke/frontend/method/State。

0f3e7197808ae288bc2b0216ef28cab2.png

4.不用vscode调试

cmd运行以下命令,监听5001端口

daprd run -dapr-http-port 3501 -app-port 5001  -app-id frontend -placement-host-address localhost:6050 -components-path C:\Users\chesterychen\.dapr\components

然后直接vs运行项目,即可调试

最后

以上就是激情小猫咪为你收集整理的Dapr + .NET 实战(九)本地调试的全部内容,希望文章能够帮你解决Dapr + .NET 实战(九)本地调试所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部