MQTTNET-为.NET 6最小API创建服务

发布于 2025-01-31 17:36:18 字数 942 浏览 3 评论 0原文

有人可以提供示例代码来创建.NET 6.0中API控制器使用的可注射的MQTTNET服务吗?我已经做了这样的事情:

MqttClientOptionsBuilder mqbuilder = new MqttClientOptionsBuilder()
        .WithClientId("VTSCMgmntC")
        .WithTcpServer("192.168.1.75", 1883);

ManagedMqttClientOptions mqoptions = new ManagedMqttClientOptionsBuilder()
       .WithAutoReconnectDelay(TimeSpan.FromSeconds(60))
       .WithClientOptions(mqbuilder.Build())
       .Build();

builder.Services.AddSingleton<IManagedMqttClient>(new MqttFactory().CreateManagedMqttClient());

在控制器方面,我有:

 private readonly VTSCMContext _context;
 private readonly IManagedMqttClient _mqttclient;

    public VTSCMgmtController(VTSCMContext context, IManagedMqttClient mqttclient)
    {
        _context = context;
        _mqttclient = mqttclient;
    }

    [HttpGet]
    public void pubtest()
    {
        _mqttclient.PublishAsync("test");
    }

显然什么都没有出版。

Could anybody provide example code to create injectable MQTTnet service to be used by API Controller in .net 6.0? I've done something like this:

MqttClientOptionsBuilder mqbuilder = new MqttClientOptionsBuilder()
        .WithClientId("VTSCMgmntC")
        .WithTcpServer("192.168.1.75", 1883);

ManagedMqttClientOptions mqoptions = new ManagedMqttClientOptionsBuilder()
       .WithAutoReconnectDelay(TimeSpan.FromSeconds(60))
       .WithClientOptions(mqbuilder.Build())
       .Build();

builder.Services.AddSingleton<IManagedMqttClient>(new MqttFactory().CreateManagedMqttClient());

and on the controller side, I've got:

 private readonly VTSCMContext _context;
 private readonly IManagedMqttClient _mqttclient;

    public VTSCMgmtController(VTSCMContext context, IManagedMqttClient mqttclient)
    {
        _context = context;
        _mqttclient = mqttclient;
    }

    [HttpGet]
    public void pubtest()
    {
        _mqttclient.PublishAsync("test");
    }

Obviously nothing gets published at all....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-02-07 17:36:18

添加用于将来参考的代码:

IMqttClientOptions options = new MqttClientOptionsBuilder()
        .WithClientId("")
        .WithTcpServer("", 1883)
        .WithCredentials("", "")
        .Build();

IMqttClient mqttclient = new MqttFactory().CreateMqttClient();

var connection =  mqttclient.ConnectAsync(options, CancellationToken.None);

connection.Wait();
var res = connection.Result;

builder.Services.AddSingleton<IMqttClient>(mqttclient);

Adding the code that worked for future reference:

IMqttClientOptions options = new MqttClientOptionsBuilder()
        .WithClientId("")
        .WithTcpServer("", 1883)
        .WithCredentials("", "")
        .Build();

IMqttClient mqttclient = new MqttFactory().CreateMqttClient();

var connection =  mqttclient.ConnectAsync(options, CancellationToken.None);

connection.Wait();
var res = connection.Result;

builder.Services.AddSingleton<IMqttClient>(mqttclient);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文