public static void AddMqttClient(this IServiceCollection services, MqttClientSetting setting) { services.AddTransient(options => new MqttClientOptionsBuilder() .WithClientId(setting.MqttBroker) .WithTcpServer((setting.MqttBroker == "built-in" || string.IsNullOrEmpty(setting.MqttBroker)) ? "127.0.0.1" : setting.MqttBroker, setting.Port) .WithCredentials(setting.UserName, setting.Password) .WithCleanSession() .Build()); }
public static void AddMqttClient(this IServiceCollection services, MqttClientSetting setting) { if (setting == null) { setting = new MqttClientSetting(); } services.AddSingleton(options => new MQTTnet.MqttFactory().CreateMqttClient()); services.AddTransient(options => new MqttClientOptionsBuilder() .WithClientId("buind-in") .WithTcpServer((setting.MqttBroker == "built-in" || string.IsNullOrEmpty(setting.MqttBroker)) ? "127.0.0.1" : setting.MqttBroker, setting.Port) .WithCredentials(setting.UserName, setting.Password) .WithCleanSession() //.WithProtocolVersion (MQTTnet.Formatter.MqttProtocolVersion.V500) .Build()); services.AddHostedService <MqttClientService>(); }