Dapr + .NET Core实战(九)本地调试

  • A+
所属分类:.NET技术
摘要

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

前几节开发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

Dapr + .NET Core实战(九)本地调试

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

Dapr + .NET Core实战(九)本地调试

 

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运行项目,即可调试