使用Dapr 編寫的是一個多進程的程序, 兩個進程之間依賴于啟動順序來組成父子進程,使用Visual Studio 調試起來可能會比較困難,因為 Visual Studio 默認只會把你當前設置的啟動項目的啟動調試。好在有Visual Studio 擴展(Microsoft Child Process Debugging Power Tool 插件)可以支持。這個思路來自 https://github.com/dapr/dotnet-sdk/issues/401#issuecomment-747563695
1、需要安裝? PowerShell 7 / Core ?(可使用命令行:dotnet tool install --global PowerShell
)
2、需要安裝 Visual Studio 擴展?Microsoft Child Process Debugging Power Tool 2022
安裝插件后啟動 Visual Studio,可以在 Debug -> Other Debugging Targets 中找到 Child Process Debugging Settings。
然后你可以按照下圖的設置開啟此項目的子進程調試:
我這里用的一個示例程序是 https://github.com/geffzhang/lab-dapr-with-famous-actors ,結合Dapr 和 Orleans 7.0 的一個Demo程序。
3、項目調試屬性設置
保存后會自動生成文件 launchSettings.json
launchSettings.json 內容也可以通過文件直接修改,效果等同,文件如下:
"Dapr-PWSH": {
?? "commandName": "Executable",
?? "executablePath": "pwsh",
?? "commandLineArgs": "-Command \"dapr run --app-id modDaprWithFamousActors --app-port 5000 --app-protocol grpc --log-level debug -- dotnet run --no-build\"",
?? "workingDirectory": ".",
?? "environmentVariables": {
???? "ASPNETCORE_ENVIRONMENT": "Development"
?? },
?? "nativeDebugging": true,
?? "dotnetRunMessages": "true",
?? "applicationUrl": "http://localhost:5000;https://localhost:5001"
}
現在,你只需要開始調試你的程序,那么你程序中啟動的新的子進程都將可以自動加入調試。
效果如下:
值得注意的是,只要啟動了本機代碼調試,就不能在程序暫停之后修改代碼了(像平時調試純托管代碼那樣),還有一個方案是sidekick