Browse Source

Refactoring

release/3.x.x
Christian Kratky 7 years ago
parent
commit
587622af81
2 changed files with 30 additions and 14 deletions
  1. +14
    -14
      Frameworks/MQTTnet.NetStandard/MqttFactory.cs
  2. +16
    -0
      Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs

+ 14
- 14
Frameworks/MQTTnet.NetStandard/MqttFactory.cs View File

@@ -16,20 +16,6 @@ namespace MQTTnet
{
private readonly IServiceProvider _serviceProvider;

private static IServiceProvider BuildServiceProvider()
{
var serviceProvider = new ServiceCollection()
.AddMqttClient()
.AddMqttServer()
.AddLogging()
.BuildServiceProvider();

serviceProvider.GetRequiredService<ILoggerFactory>()
.AddMqttTrace();

return serviceProvider;
}

public MqttFactory()
: this(BuildServiceProvider())
{
@@ -128,5 +114,19 @@ namespace MQTTnet

return _serviceProvider.GetRequiredService<IMqttServer>();
}

private static IServiceProvider BuildServiceProvider()
{
var serviceProvider = new ServiceCollection()
.AddMqttClient()
.AddMqttServer()
.AddLogging()
.BuildServiceProvider();

serviceProvider.GetRequiredService<ILoggerFactory>()
.AddMqttTrace();

return serviceProvider;
}
}
}

+ 16
- 0
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs View File

@@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
using MQTTnet.Core;
using MQTTnet.Core.Client;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.ManagedClient;
using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server;
using MQTTnet.Implementations;
@@ -454,6 +455,21 @@ namespace MQTTnet.TestApp.UniversalWindows
var mqttServer = new MqttFactory().CreateMqttServer();
}

{
// Setup and start a managed MQTT client.
var options = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(new MqttClientOptionsBuilder()
.WithClientId("Client1")
.WithTcpServer("broker.hivemq.com")
.WithTls().Build())
.Build();

var mqttClient = new MqttFactory().CreateManagedMqttClient();
await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
await mqttClient.StartAsync(options);
}

}

private async void StartServer(object sender, RoutedEventArgs e)


Loading…
Cancel
Save