Dapr + .NET Core實戰(九)本地調試

前幾節開發Dapr應用程序時,我們使用 dapr cli 來啓動dapr服務,就像這樣:

dapr run --dapr-http-port 3501 --app-port 5001  --app-id frontend dotnet  .\FrontEnd\bin\Debug\net5.0\FrontEnd.dll

如果你想要通過dapr調試服務呢?在這裏使用 dapr 運行時(daprd) 來幫助實現這一點。具體原理就是先從命令行中運行符合正確參數的 daprd,然後啓動您的代碼並附加調試器。

1.配置launch.json

vscode打開項目,並創建launch.json

 修改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。

 

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運行項目,即可調試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章