MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Question: How to use multiple endpoints?

Open chris1705 opened this issue 8 years ago • 2 comments

Hello from Germany!

At first I'd like to thank you for sharing your project!

I'm currently trying to use your MQTT broker with two endpoints ("plain" TCP and Websocket). How can I use both at once without creating multiple instances and syncing them?

Regards Chris

chris1705 avatar Feb 19 '18 19:02 chris1705

Hello :)

I think, I've found I solution by myself. I have taken a look at your ServiceCollectionExtensions and created a custom one for me. It looks like the following:

public static IServiceCollection E(this IServiceCollection services, IMqttServerOptions options)
        {
            if (options == null) throw new ArgumentNullException(nameof(options));

            services.AddSingleton(options);
            services.AddSingleton<IMqttNetLogger>(new MqttNetLogger());
            services.AddSingleton<MqttHostedServer>();
            services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
            services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());

            services.AddSingleton<MqttWebSocketServerAdapter>();
            services.AddSingleton<MqttServerAdapter>();
            services.AddSingleton<IEnumerable<IMqttServerAdapter>>(s =>
            {
                List<IMqttServerAdapter> adapters = new List<IMqttServerAdapter>();
                adapters.Add(s.GetService<MqttServerAdapter>());
                adapters.Add(s.GetService<MqttWebSocketServerAdapter>());
                return adapters;
            });

            return services;
        }

Please let me know, if there is a nicer way, as I'm not into the dependency injection framework of .net Core. :)

Regards Chris

chris1705 avatar Feb 19 '18 21:02 chris1705

Hi @chris1705, at the moment there is no other way as the one you found by yourself. So it is the best one 😄

Best regards Christian

chkr1011 avatar Feb 19 '18 22:02 chkr1011