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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Dapr pubsub 中,可通过在订阅函数中添加幂等性判断来解决重复消费的问题。以下是一个基于 .NET 6 的幂等性实现示例: ```csharp using Dapr.Client; using Dapr.Client.Autogen.Grpc.v1; using Dapr.Client.Autogen.Protos; using Google.Protobuf; using Grpc.Core; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace DaprPubSubDemo.Controllers { [ApiController] [Route("[controller]")] public class DaprPubSubController : ControllerBase { private readonly DaprClient _daprClient; public DaprPubSubController(DaprClient daprClient) { _daprClient = daprClient; } [HttpPost("subscribe")] public async Task<ActionResult> Subscribe([FromBody] CloudEvent cloudEvent) { // 获取消息 ID string messageId = cloudEvent.Id; // 判断消息是否已处理过 if (await _daprClient.GetStateEntryAsync<bool>("message", messageId) == false) { // 标记消息已处理 await _daprClient.SaveStateAsync("message", messageId, true); // 处理消息 // TODO: 在此处添加具体的消息处理逻辑 } return Ok(); } } } ``` 上述代码中,我们使用 Dapr 的状态管理功能来实现幂等性判断。在订阅函数中,首先获取消息的 ID,然后通过调用 `GetStateEntryAsync` 方法获取该消息的处理状态。如果消息尚未处理,则标记消息已处理,并执行具体的消息处理逻辑;否则,直接返回结果。 需要注意的是,上述代码中的状态存储使用的是 Dapr 默认的状态存储,可以根据实际需求进行修改。同时,还需要注意保证状态存储的可靠性和一致性,以确保幂等性判断的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值