Visual Studio Code manual debugging configuration

How to manually setup Visual Studio Code debugging

The Dapr VSCode extension automates the setup of VSCode debugging.

If instead you wish to manually configure the [tasks.json](https://code.visualstudio.com/Docs/editor/tasks) and [launch.json](https://code.visualstudio.com/Docs/editor/debugging) files to use Dapr, these are the steps.

When developing Dapr applications, you typically use the Dapr cli to start your daprized service similar to this:

  1. dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 app.js

One approach to attaching the debugger to your service is to first run daprd with the correct arguments from the command line and then launch your code and attach the debugger. While this is a perfectly acceptable solution, it does require a few extra steps and some instruction to developers who might want to clone your repo and hit the “play” button to begin debugging.

Using the tasks.json and launch.json files in Visual Studio Code, you can simplify the process and request that VS Code kick off the daprd process prior to launching the debugger.

Modifying launch.json configurations to include a preLaunchTask

In your launch.json file add a preLaunchTask for each configuration that you want daprd launched. The preLaunchTask references tasks that you define in your tasks.json file. Here is an example for both Node and .NET Core. Notice the preLaunchTasks referenced: daprd-web and daprd-leaderboard.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "type": "node",
  6. "request": "launch",
  7. "name": "Node Launch w/Dapr (Web)",
  8. "preLaunchTask": "daprd-web",
  9. "program": "${workspaceFolder}/Game/Web/server.js",
  10. "skipFiles": [
  11. "<node_internals>/**"
  12. ]
  13. },
  14. {
  15. "type": "coreclr",
  16. "request": "launch",
  17. "name": ".NET Core Launch w/Dapr (LeaderboardService)",
  18. "preLaunchTask": "daprd-leaderboard",
  19. "program": "${workspaceFolder}/Game/Services/LeaderboardService/bin/Debug/netcoreapp3.0/LeaderboardService.dll",
  20. "args": [],
  21. "cwd": "${workspaceFolder}/Game/Services/LeaderboardService",
  22. "stopAtEntry": false,
  23. "serverReadyAction": {
  24. "action": "openExternally",
  25. "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
  26. },
  27. "env": {
  28. "ASPNETCORE_ENVIRONMENT": "Development"
  29. },
  30. "sourceFileMap": {
  31. "/Views": "${workspaceFolder}/Views"
  32. }
  33. }
  34. ]
  35. }

Adding daprd tasks to tasks.json

You need to define a task and problem matcher for daprd in your tasks.json file. Here are two examples (both referenced via the preLaunchTask members above). Notice that in the case of the .NET Core daprd task (daprd-leaderboard) there is also a dependsOn member that references the build task to ensure the latest code is being run/debugged. The problemMatcher is used so that VSCode can understand when the daprd process is up and running.

Let’s take a quick look at the args that are being passed to the daprd command.

  • -app-id – the id (how you locate it via service invocation) of your microservice
  • -app-port – the port number that your application code is listening on
  • -dapr-http-port – the http port for the dapr api
  • -dapr-grpc-port – the grpc port for the dapr api
  • -placement-host-address – the location of the placement service (this should be running in docker as it was created when you installed dapr and ran dapr init)

Note: You need to ensure that you specify different http/grpc (-dapr-http-port and -dapr-grpc-port) ports for each daprd task that you create, otherwise you run into port conflicts when you attempt to launch the second configuration.

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "build",
  6. "command": "dotnet",
  7. "type": "process",
  8. "args": [
  9. "build",
  10. "${workspaceFolder}/Game/Services/LeaderboardService/LeaderboardService.csproj",
  11. "/property:GenerateFullPaths=true",
  12. "/consoleloggerparameters:NoSummary"
  13. ],
  14. "problemMatcher": "$msCompile"
  15. },
  16. {
  17. "label": "daprd-web",
  18. "command": "daprd",
  19. "args": [
  20. "-app-id",
  21. "whac-a-mole--web",
  22. "-app-port",
  23. "3000",
  24. "-dapr-http-port",
  25. "51000",
  26. "-dapr-grpc-port",
  27. "52000",
  28. "-placement-host-address",
  29. "localhost:50005"
  30. ],
  31. "isBackground": true,
  32. "problemMatcher": {
  33. "pattern": [
  34. {
  35. "regexp": ".",
  36. "file": 1,
  37. "location": 2,
  38. "message": 3
  39. }
  40. ],
  41. "background": {
  42. "beginsPattern": "^.*starting Dapr Runtime.*",
  43. "endsPattern": "^.*waiting on port.*"
  44. }
  45. }
  46. },
  47. {
  48. "label": "daprd-leaderboard",
  49. "command": "daprd",
  50. "args": [
  51. "-app-id",
  52. "whac-a-mole--leaderboard",
  53. "-app-port",
  54. "5000",
  55. "-dapr-http-port",
  56. "51001",
  57. "-dapr-grpc-port",
  58. "52001",
  59. "-placement-host-address",
  60. "localhost:50005"
  61. ],
  62. "isBackground": true,
  63. "problemMatcher": {
  64. "pattern": [
  65. {
  66. "regexp": ".",
  67. "file": 1,
  68. "location": 2,
  69. "message": 3
  70. }
  71. ],
  72. "background": {
  73. "beginsPattern": "^.*starting Dapr Runtime.*",
  74. "endsPattern": "^.*waiting on port.*"
  75. }
  76. },
  77. "dependsOn": "build"
  78. }
  79. ]
  80. }

Wrapping up

Once you have made the required changes, you should be able to switch to the debug view in VSCode and launch your daprized configurations by clicking the “play” button. If everything was configured correctly, you should see daprd launch in the VSCode terminal window and the debugger should attach to your application (you should see it’s output in the debug window).

Note

Since you didn’t launch the service(s) using the dapr run cli command, but instead by running daprd, the dapr list command will not show a list of apps that are currently running.

Last modified August 2, 2021 : Fix Java SDK link (#1695) (2c67fd1)