diff --git a/Build/MQTTnet.AspNetCore.nuspec b/Build/MQTTnet.AspNetCore.nuspec new file mode 100644 index 0000000..eb5f637 --- /dev/null +++ b/Build/MQTTnet.AspNetCore.nuspec @@ -0,0 +1,30 @@ + + + + MQTTnet.AspNetCore + 2.5.0 + Christian Kratky + Christian Kratky + https://github.com/chkr1011/MQTTnet/blob/master/LICENSE + https://github.com/chkr1011/MQTTnet + https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png + false + This is a support library to integrate MQTTnet into AspNetCore. + initial version + + Copyright Christian Kratky 2016-2017 + MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 2403cc8..54c4c47 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -51,8 +51,8 @@ - - + + \ No newline at end of file diff --git a/Build/build.ps1 b/Build/build.ps1 index 70b616b..5e280e2 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -3,9 +3,11 @@ param([string]$version) if ([string]::IsNullOrEmpty($version)) {$version = "0.0.1"} $msbuild = "MSBuild.exe" -&dotnet build ..\Frameworks\MQTTnet.Netstandard\MQTTnet.Netstandard.csproj -c="Release" +&dotnet build ..\Frameworks\MQTTnet.Netstandard\MQTTnet.Netstandard.csproj -c="Release" /p:FileVersion=$version /p:AssemblyVersion=$version +&dotnet build ..\Frameworks\MQTTnet.AspNetCore\MQTTnet.AspNetCore.csproj -c="Release" /p:FileVersion=$version /p:AssemblyVersion=$version &$msbuild ..\Frameworks\MQTTnet.UniversalWindows\MQTTnet.UniversalWindows.csproj /t:Build /p:Configuration="Release" Remove-Item .\NuGet -Force -Recurse New-Item -ItemType Directory -Force -Path .\NuGet -.\NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version \ No newline at end of file +.\NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version +.\NuGet.exe pack MQTTnet.AspNetCore.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version \ No newline at end of file diff --git a/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs b/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs index a62ecde..06ca74d 100644 --- a/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs +++ b/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using MQTTnet.Core.Server; -namespace MQTTnet.AspnetCore +namespace MQTTnet.AspNetCore { public static class ApplicationBuilderExtensions { diff --git a/Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs b/Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs index b5326ab..db72573 100644 --- a/Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs +++ b/Frameworks/MQTTnet.AspnetCore/MqttHostedServer.cs @@ -7,7 +7,7 @@ using Microsoft.Extensions.Options; using MQTTnet.Core.Adapter; using MQTTnet.Core.Server; -namespace MQTTnet.AspnetCore +namespace MQTTnet.AspNetCore { public class MqttHostedServer : MqttServer, IHostedService { diff --git a/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs b/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs index abb30e3..5cd5d55 100644 --- a/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs +++ b/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs @@ -8,7 +8,7 @@ using MQTTnet.Core.Channel; using MQTTnet.Core.Server; using MQTTnet.Implementations; -namespace MQTTnet.AspnetCore +namespace MQTTnet.AspNetCore { public class MqttWebSocketServerAdapter : IMqttServerAdapter, IDisposable { diff --git a/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index 1951368..f915ab4 100644 --- a/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Hosting; using MQTTnet.Core.Adapter; using MQTTnet.Core.Server; -namespace MQTTnet.AspnetCore +namespace MQTTnet.AspNetCore { public static class ServiceCollectionExtensions { diff --git a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs index 7d88480..0ff6136 100644 --- a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs +++ b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs @@ -4,6 +4,7 @@ using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using MQTTnet.Implementations; using MQTTnet.Core.ManagedClient; using MQTTnet.Core.Server; @@ -109,5 +110,14 @@ namespace MQTTnet { return _serviceProvider.GetRequiredService(); } + + public IMqttServer CreateMqttServer(Action configure) + { + var options = _serviceProvider.GetRequiredService>(); + + configure(options.Value); + + return _serviceProvider.GetRequiredService(); + } } } \ No newline at end of file diff --git a/MQTTnet.Core/Server/IMqttServerFactory.cs b/MQTTnet.Core/Server/IMqttServerFactory.cs index 61d022f..500614b 100644 --- a/MQTTnet.Core/Server/IMqttServerFactory.cs +++ b/MQTTnet.Core/Server/IMqttServerFactory.cs @@ -1,7 +1,11 @@ -namespace MQTTnet.Core.Server +using System; + +namespace MQTTnet.Core.Server { public interface IMqttServerFactory { IMqttServer CreateMqttServer(); + + IMqttServer CreateMqttServer(Action configure); } } \ No newline at end of file diff --git a/MQTTnet.sln b/MQTTnet.sln index 263fee0..aae1e6e 100644 --- a/MQTTnet.sln +++ b/MQTTnet.sln @@ -20,6 +20,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{67C28AC1-BC3A-420A-BE9C-FA2401431CF9}" ProjectSection(SolutionItems) = preProject Build\build.ps1 = Build\build.ps1 + Build\MQTTnet.AspNetCore.nuspec = Build\MQTTnet.AspNetCore.nuspec Build\MQTTnet.nuspec = Build\MQTTnet.nuspec EndProjectSection EndProject @@ -33,7 +34,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.NetCore", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.AspNetCore2", "Tests\MQTTnet.TestApp.AspNetCore2\MQTTnet.TestApp.AspNetCore2.csproj", "{C6FF8AEA-0855-41EC-A1F3-AC262225BAB9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.AspnetCore", "Frameworks\MQTTnet.AspnetCore\MQTTnet.AspnetCore.csproj", "{F10C4060-F7EE-4A83-919F-FF723E72F94A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.AspNetCore", "Frameworks\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj", "{F10C4060-F7EE-4A83-919F-FF723E72F94A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs index d548ff0..4dd9f3a 100644 --- a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs @@ -51,23 +51,33 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_WillMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); - await s.StartAsync(); - - var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build(); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; - c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)); + try + { + await s.StartAsync(); - await c2.DisconnectAsync(); + var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build(); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage); - await Task.Delay(1000); + c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)); - await s.StopAsync(); + await c2.DisconnectAsync(); + await Task.Delay(1000); + } + finally + { + await s.StopAsync(); + } Assert.AreEqual(1, receivedMessagesCount); } @@ -75,33 +85,45 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Unsubscribe() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); - await s.StartAsync(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; - c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build(); + try + { + await s.StartAsync(); - await c2.PublishAsync(message); - Assert.AreEqual(0, receivedMessagesCount); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce)); - await c2.PublishAsync(message); + var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build(); - await Task.Delay(500); - Assert.AreEqual(1, receivedMessagesCount); + await c2.PublishAsync(message); + Assert.AreEqual(0, receivedMessagesCount); - await c1.UnsubscribeAsync("a"); - await c2.PublishAsync(message); + await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce)); + await c2.PublishAsync(message); - await Task.Delay(500); - Assert.AreEqual(1, receivedMessagesCount); + await Task.Delay(500); + Assert.AreEqual(1, receivedMessagesCount); + + await c1.UnsubscribeAsync("a"); + await c2.PublishAsync(message); - await s.StopAsync(); + await Task.Delay(500); + Assert.AreEqual(1, receivedMessagesCount); + } + finally + { + await s.StopAsync(); + } await Task.Delay(500); Assert.AreEqual(1, receivedMessagesCount); @@ -111,22 +133,34 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_Publish() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); - await s.StartAsync(); - - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; - c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build(); - await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce)); + try + { + await s.StartAsync(); + + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - s.PublishAsync(message).Wait(); - await Task.Delay(500); + c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await s.StopAsync(); + var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build(); + await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce)); + s.PublishAsync(message).Wait(); + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } + Assert.AreEqual(1, receivedMessagesCount); } @@ -134,21 +168,33 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_NoRetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); - await s.StartAsync(); - - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build()); - await c1.DisconnectAsync(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + var s = new MqttFactory(services).CreateMqttServer(); var receivedMessagesCount = 0; - c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); - await Task.Delay(500); + try + { + await s.StartAsync(); + + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build()); + await c1.DisconnectAsync(); - await s.StopAsync(); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); + + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } Assert.AreEqual(0, receivedMessagesCount); } @@ -157,22 +203,34 @@ namespace MQTTnet.Core.Tests public async Task MqttServer_RetainedMessage() { var serverAdapter = new TestMqttServerAdapter(); - var s = new MqttFactory().CreateMqttServer(); - await s.StartAsync(); + var services = new ServiceCollection() + .AddLogging() + .AddMqttServer() + .AddSingleton(serverAdapter) + .BuildServiceProvider(); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); - await c1.DisconnectAsync(); + var s = new MqttFactory(services).CreateMqttServer(); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; - c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); + try + { + await s.StartAsync(); - await Task.Delay(500); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); + await c1.DisconnectAsync(); - await s.StopAsync(); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } + Assert.AreEqual(1, receivedMessagesCount); } @@ -182,26 +240,32 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer() // TODO: Is there maybe an easier way for the library user to set the options? + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); var s = new MqttFactory(services).CreateMqttServer(); - await s.StartAsync(); + var receivedMessagesCount = 0; + try + { + await s.StartAsync(); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); - await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build()); - await c1.DisconnectAsync(); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build()); + await c1.DisconnectAsync(); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); - var receivedMessagesCount = 0; - c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); - await Task.Delay(500); + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } - await s.StopAsync(); Assert.AreEqual(0, receivedMessagesCount); } @@ -214,30 +278,42 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer(options => options.Storage = storage) // TODO: Is there maybe an easier way for the library user to set the options? + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); - var s = new MqttFactory(services).CreateMqttServer(); // TODO: Like here? - await s.StartAsync(); + var s = new MqttFactory(services).CreateMqttServer(options => options.Storage = storage); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); - await c1.DisconnectAsync(); + try + { + await s.StartAsync(); - await s.StopAsync(); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build()); + await c1.DisconnectAsync(); + } + finally + { + await s.StopAsync(); + } s = services.GetRequiredService(); - await s.StartAsync(); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); var receivedMessagesCount = 0; - c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); + try + { + await s.StartAsync(); - await Task.Delay(500); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce)); - await s.StopAsync(); + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } Assert.AreEqual(1, receivedMessagesCount); } @@ -254,30 +330,37 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddLogging() - .AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor) + .AddMqttServer() .AddSingleton(serverAdapter) .BuildServiceProvider(); - var s = services.GetRequiredService(); - await s.StartAsync(); + var s = new MqttFactory(services).CreateMqttServer(options => options.ApplicationMessageInterceptor = Interceptor); + try + { + await s.StartAsync(); - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); - await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build()); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build()); - var isIntercepted = false; - c2.ApplicationMessageReceived += (sender, args) => - { - isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0; - }; + var isIntercepted = false; + c2.ApplicationMessageReceived += (sender, args) => + { + isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0; + }; - var m = new MqttApplicationMessageBuilder().WithTopic("test").Build(); - await c1.PublishAsync(m); - await c1.DisconnectAsync(); + var m = new MqttApplicationMessageBuilder().WithTopic("test").Build(); + await c1.PublishAsync(m); + await c1.DisconnectAsync(); - await Task.Delay(500); + await Task.Delay(500); - Assert.IsTrue(isIntercepted); + Assert.IsTrue(isIntercepted); + } + finally + { + await s.StopAsync(); + } } private class TestStorage : IMqttServerStorage @@ -306,28 +389,34 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var services = new ServiceCollection() .AddMqttServer() + .AddLogging() .AddSingleton(serverAdapter) .BuildServiceProvider(); var s = services.GetRequiredService(); - await s.StartAsync(); - - var c1 = await serverAdapter.ConnectTestClient(s, "c1"); - var c2 = await serverAdapter.ConnectTestClient(s, "c2"); - var receivedMessagesCount = 0; - c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; + try + { + await s.StartAsync(); - await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build()); - await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()); + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); - await Task.Delay(500); - await c1.UnsubscribeAsync(topicFilter); + c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++; - await Task.Delay(500); + await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build()); + await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()); - await s.StopAsync(); + await Task.Delay(500); + await c1.UnsubscribeAsync(topicFilter); + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } + Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount); } } diff --git a/Tests/MQTTnet.Core.Tests/TestLogger.cs b/Tests/MQTTnet.Core.Tests/TestLogger.cs index ea597a6..f57c877 100644 --- a/Tests/MQTTnet.Core.Tests/TestLogger.cs +++ b/Tests/MQTTnet.Core.Tests/TestLogger.cs @@ -3,11 +3,11 @@ using System; namespace MQTTnet.Core.Tests { - public class TestLogger : ILogger + public class TestLogger : IDisposable, ILogger { public IDisposable BeginScope(TState state) { - throw new NotImplementedException(); + return this; } public bool IsEnabled(LogLevel logLevel) @@ -18,5 +18,9 @@ namespace MQTTnet.Core.Tests public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { } + + public void Dispose() + { + } } } diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj index 2f04208..7e4e0e3 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj +++ b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj @@ -14,7 +14,7 @@ - +