dotnet / dotnet/MQTTnet

Client connected to broker but controller throws InvalidOperationException: The MQTT server is not started

Open
#1,893 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Program.cs:

```
var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(o =>
{
o.ListenAnyIP(1883, l => l.UseMqtt());
o.ListenAnyIP(5000);
});

var optionBuilder = new MqttServerOptionsBuilder()
.WithoutDefaultEndpoint()
.Build();

builder.Services.AddControllers();
builder.Services.AddHostedMqttServer(optionBuilder);
builder.Services.AddMqttConnectionHandler();
builder.Services.AddConnections();

var app = builder.Build();

app.UseAuthorization();
app.MapControllers();
app.Run();

```
StatusController.cs:

```
private readonly MqttHostedServer _mqttServer;

public StatusController(MqttHostedServer mqttServer)
{
_mqttServer = mqttServer;
}
[HttpGet]
public async Task GetConnectedClientsAsync()
{
var clients = await _mqttServer.GetClientsAsync();
return Ok(clients);
}
```

Line `var clients = await _mqttServer.GetClientsAsync();` throws InvalidOperationException: The MQTT server is not started, but client can connect successufuly. Do I have two instances of Mqtt server in my app? How to do it in .NET 6? I saw documentation and samples for .NET 5 but I can't make it work.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.