Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamfierek opened this issue Dec 6, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@adamfierek
Copy link

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<IActionResult> 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.

@adamfierek adamfierek added the bug Something isn't working label Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant