question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

asp.net core 2.1 - ConfigureServices - How to build-up MQTT Server Options

See original GitHub issue

Considering this code:

            var mqttServerOptions = new MqttServerOptionsBuilder()
                .WithDefaultEndpoint()
                .WithDefaultEndpointPort(9876)
                .WithConnectionValidator((ctx) =>
                {
                    var mediator = serviceProvider.GetService<IMediator>();
                    if (await mediator.Send(new AuthenticateTerminalForMqttCommand(ctx.Username, ctx.Password)))
                    {
                        logger.LogInformation("Client {0} has connected.", ctx.ClientId);
                        ctx.ReturnCode = MqttConnectReturnCode.ConnectionAccepted;
                        return;
                    }

                    logger.LogInformation("Client {0} has failed to authenticate.", ctx.ClientId);
                    ctx.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
                })
                .Build();

We cannot pull anything into ConfigureServices() but the IServiceCollection object. So, with asp.net core 2.1, how do we provide a connection validator, among other items? When I look at the main Configure() method, although we can do app.UseMqttServer((mqttServer) => { ... custom code here ... });, it does not seem to let us assign a custom connection validator, as the property is marked as ReadOnly at this point.

Any suggestions/ideas for how to overcome this want/need would be great.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JanEggerscommented, Sep 8, 2018

the problem is that the new pipeline does not yet support encrypted communication. (there is no tls connection middleware yet and using sslstream negates all the performance benefits)

so you should remove

        o.ListenAnyIP(9010, l => l.UseMqtt()); // mqtt pipeline
        services.AddMqttConnectionHandler();

and instead add the old tcp server adapter with

  services.AddMqttTcpServerAdapter();
1reaction
dealproccommented, Aug 21, 2018

That’s pretty much what I wound up with. I now need to go through the bowels of the wire-up and see if its going to be possible or not to build an orleans backplane for this, so that we can host this within a web farm environment. I know others have advised against this, but I’m trying to keep our stack as compact as possible until we hit a critical mass.

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core 2.1 - ConfigureServices - How to build-up ...
Considering this code: var mqttServerOptions = new MqttServerOptionsBuilder() .WithDefaultEndpoint() .WithDefaultEndpointPort(9876) .
Read more >
Server · dotnet/MQTTnet Wiki
MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation...
Read more >
how to use GetConnectedClientsAsync from MQTTnet. ...
I can host mqtt broker with MQTTnet nuget in asp.net core 2.2 by following code. I just want to show list of connected...
Read more >
NET Generic Host in ASP.NET Core
The ConfigureWebHostDefaults method: Loads host configuration from environment variables prefixed with ASPNETCORE_ . Sets Kestrel server as the ...
Read more >
Creating a Daemon with .NET Core (Part 2)
Now, you can build and run the app. To do so, first run dotnet build in the root directory of the app. Next,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found