From 6edbd589187d7ccdc2c756a908f22a95f59e515d Mon Sep 17 00:00:00 2001 From: Dhurim Kelmendi Date: Wed, 22 Dec 2021 10:50:55 +0100 Subject: [PATCH] Upgrade MQTTNet to latest version, add support for .NET5 --- .gitignore | 3 + CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln | 82 +++++++++---------- azure-pipelines.yml | 64 +++++++++++++++ .../Bindings/MqttConfigurationParser.cs | 8 +- .../MqttTriggerAttributeBindingProvider.cs | 4 +- .../Bindings/MqttTriggerBinding.cs | 8 +- ...nline.Azure.WebJobs.Extensions.Mqtt.csproj | 17 ++-- .../Config/MqttConnectionFactory.cs | 8 +- .../Listeners/IMqttConnection.cs | 2 +- .../Listeners/MqttConnection.cs | 8 +- .../Listeners/MqttListener.cs | 4 +- .../MqttTriggerAttribute.cs | 13 ++- src/ExampleFunctions/ExampleFunctions.csproj | 10 ++- ...Azure.WebJobs.Extensions.Mqtt.Tests.csproj | 20 +++-- .../EndToEnd/TriggerTests.cs | 2 +- .../Helpers/JobHostHelper.cs | 2 +- .../Helpers/MqttClientHelper.cs | 2 +- .../Helpers/MqttLogger.cs | 16 +++- .../Helpers/MqttServerHelper.cs | 2 +- .../MqttConnectionTests.cs | 12 +-- .../MqttListenerTests.cs | 18 ++-- 21 files changed, 191 insertions(+), 114 deletions(-) create mode 100644 azure-pipelines.yml diff --git a/.gitignore b/.gitignore index 43322df..3680f59 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,9 @@ bld/ [Oo]bj/ [Ll]og/ +# Editor config +.editorconfig + # Visual Studio 2015 cache/options directory .vs/ # Uncomment if you have tasks that create the project's static files in wwwroot diff --git a/CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln b/CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln index 15aa3c3..e1405c5 100644 --- a/CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln +++ b/CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln @@ -1,12 +1,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27004.2005 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaseOnline.Azure.WebJobs.Extensions.Mqtt", "src\CaseOnline.Azure.WebJobs.Extensions.Mqtt\CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj", "{EA475418-65F6-49A1-A99C-EAC1031A3457}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaseOnline.Azure.WebJobs.Extensions.Mqtt", "src\CaseOnline.Azure.WebJobs.Extensions.Mqtt\CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj", "{02D92D5F-E9FA-4816-9271-B07545DD083F}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleFunctions", "src\ExampleFunctions\ExampleFunctions.csproj", "{B8C8DF7D-9439-4111-AEA1-A0837125F8AB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleFunctions", "src\ExampleFunctions\ExampleFunctions.csproj", "{12D92D5F-E9FA-4816-9271-B07545DD0840}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests", "test\CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests\CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj", "{51601B5F-5429-4A18-A3A4-CC31BB705FB3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests", "test\CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests\CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj", "{DC968091-3FBF-4B78-A60F-E4818DF0022E}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BAEA76D9-A6EC-455A-8641-5165BA07DDE6}" ProjectSection(SolutionItems) = preProject @@ -25,42 +25,42 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|x64.ActiveCfg = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|x64.Build.0 = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|x86.ActiveCfg = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Debug|x86.Build.0 = Debug|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|Any CPU.Build.0 = Release|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|x64.ActiveCfg = Release|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|x64.Build.0 = Release|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|x86.ActiveCfg = Release|Any CPU - {EA475418-65F6-49A1-A99C-EAC1031A3457}.Release|x86.Build.0 = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|x64.ActiveCfg = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|x64.Build.0 = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|x86.ActiveCfg = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Debug|x86.Build.0 = Debug|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|Any CPU.Build.0 = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|x64.ActiveCfg = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|x64.Build.0 = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|x86.ActiveCfg = Release|Any CPU - {B8C8DF7D-9439-4111-AEA1-A0837125F8AB}.Release|x86.Build.0 = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|x64.ActiveCfg = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|x64.Build.0 = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|x86.ActiveCfg = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Debug|x86.Build.0 = Debug|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|Any CPU.Build.0 = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|x64.ActiveCfg = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|x64.Build.0 = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|x86.ActiveCfg = Release|Any CPU - {51601B5F-5429-4A18-A3A4-CC31BB705FB3}.Release|x86.Build.0 = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|x64.ActiveCfg = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|x64.Build.0 = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|x86.ActiveCfg = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Debug|x86.Build.0 = Debug|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|Any CPU.Build.0 = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|x64.ActiveCfg = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|x64.Build.0 = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|x86.ActiveCfg = Release|Any CPU + {02D92D5F-E9FA-4816-9271-B07545DD083F}.Release|x86.Build.0 = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|x64.ActiveCfg = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|x64.Build.0 = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|x86.ActiveCfg = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Debug|x86.Build.0 = Debug|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|Any CPU.Build.0 = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|x64.ActiveCfg = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|x64.Build.0 = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|x86.ActiveCfg = Release|Any CPU + {12D92D5F-E9FA-4816-9271-B07545DD0840}.Release|x86.Build.0 = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|x64.ActiveCfg = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|x64.Build.0 = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|x86.ActiveCfg = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Debug|x86.Build.0 = Debug|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|Any CPU.Build.0 = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|x64.ActiveCfg = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|x64.Build.0 = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|x86.ActiveCfg = Release|Any CPU + {DC968091-3FBF-4B78-A60F-E4818DF0022E}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..4267391 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,64 @@ +name: 1.0$(rev:.r) + +trigger: +- master +- main + +pool: + vmImage: 'windows-latest' + +variables: + solution: '**/*.sln' + buildPlatform: 'Any CPU' + buildConfiguration: 'Release' + +stages: +- stage: PublishPackage + displayName: Test and publish package + variables: + - group: variables-devops + jobs: + - job: + displayName: Test and publish package + steps: + - checkout: self + path: s + + - task: DotNetCoreCLI@2 + displayName: 'Restoring packages' + inputs: + command: 'restore' + projects: '**/**/*.csproj' + feedsToUse: 'select' + vstsFeed: '$(vstsFeed)' + + - task: DotNetCoreCLI@2 + displayName: 'Building the assemblies' + inputs: + command: 'build' + projects: '**/**/*.csproj' + versioningScheme: 'byBuildNumber' + arguments: '--configuration $(BuildConfiguration)' + + - task: DotNetCoreCLI@2 + displayName: 'Testing the assemblies' + inputs: + command: 'test' + projects: '**/**/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj' + + - task: DotNetCoreCLI@2 + displayName: 'Creating the package' + inputs: + command: 'pack' + packagesToPack: '**/**/*.csproj' + packDirectory: 'dist' + nobuild: true + versioningScheme: 'byBuildNumber' + + - task: DotNetCoreCLI@2 + displayName: 'Staging the package' + inputs: + command: 'push' + packagesToPush: 'dist/*.nupkg' + nuGetFeedType: 'internal' + publishVstsFeed: '$(vstsFeed)' \ No newline at end of file diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs index 2e6bbf2..b128409 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttConfigurationParser.cs @@ -90,19 +90,19 @@ private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionS if (connectionString.Tls) { - var certificates = new List(); + var certificates = new List(); if (connectionString.Certificate != null) { - using (var cert = new X509Certificate(connectionString.Certificate)) + using (var cert = new X509Certificate2(connectionString.Certificate)) { - var serializedServerCertificate = cert.Export(X509ContentType.Cert); - certificates.Add(serializedServerCertificate); + certificates.Add(cert); } } mqttClientOptionsBuilder = mqttClientOptionsBuilder.WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = true, + SslProtocol = System.Security.Authentication.SslProtocols.Tls12, Certificates = certificates, #if DEBUG CertificateValidationCallback = (X509Certificate x, X509Chain y, SslPolicyErrors z, IMqttClientOptions o) => diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs index be0f918..c48418b 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerAttributeBindingProvider.cs @@ -84,7 +84,7 @@ private static MqttTriggerAttribute GetMqttTriggerAttribute(ParameterInfo parame private ITriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute) { - var topicFilters = new List(); + var topicFilters = new List(); var mqttConnection = _connectionFactory.GetMqttConnection(mqttTriggerAttribute); try @@ -98,7 +98,7 @@ private ITriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTrigg topicFilters.AddRange(mqttTriggerAttribute.TopicStrings.Select(t => { var topicString = (mqttTriggerAttribute.MqttConfigCreatorType != null) ? _nameResolver.ResolveWholeString(t) : t; - return new TopicFilter() { Topic = topicString, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce }; + return new MqttTopicFilter() { Topic = topicString, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce }; })); } } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerBinding.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerBinding.cs index 4696892..60d5583 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerBinding.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Bindings/MqttTriggerBinding.cs @@ -20,7 +20,7 @@ public class MqttTriggerBinding : ITriggerBinding { private readonly ParameterInfo _parameter; private readonly MqttConnection _connection; - private readonly TopicFilter[] _topics; + private readonly MqttTopicFilter[] _topics; private readonly ILogger _logger; private readonly IReadOnlyDictionary _emptyBindingData = new Dictionary(); @@ -31,7 +31,7 @@ public class MqttTriggerBinding : ITriggerBinding /// The MQTT connection. /// The topics to subscribe to. /// The logger. - public MqttTriggerBinding(ParameterInfo parameter, MqttConnection connection, TopicFilter[] topics, ILogger logger) + public MqttTriggerBinding(ParameterInfo parameter, MqttConnection connection, MqttTopicFilter[] topics, ILogger logger) { _parameter = parameter; _connection = connection; @@ -82,7 +82,7 @@ public ParameterDescriptor ToParameterDescriptor() return descriptor; } - private class MqttTriggerParameterDescriptor : TriggerParameterDescriptor + private sealed class MqttTriggerParameterDescriptor : TriggerParameterDescriptor { public override string GetTriggerReason(IDictionary arguments) { @@ -90,7 +90,7 @@ public override string GetTriggerReason(IDictionary arguments) } } - private class ValueProvider : IValueProvider + private sealed class ValueProvider : IValueProvider { private readonly object _value; diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj index a32ed3e..aa796f7 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/CaseOnline.Azure.WebJobs.Extensions.Mqtt.csproj @@ -1,6 +1,6 @@  - netstandard2.0 + net5.0 Kees Schollaart Case Online true @@ -45,23 +45,20 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin - - - + + + all runtime; build; native; contentfiles; analyzers All - - All - - - - + + + diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs index 96877ce..133978c 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Config/MqttConnectionFactory.cs @@ -48,15 +48,15 @@ internal bool AllConnectionsConnected() public async Task DisconnectAll() { - foreach (var connection in _mqttConnections) + foreach (var mqttConnection in _mqttConnections.Select(connection => connection.Value.MqttConnection)) { - await connection.Value.MqttConnection.StopAsync().ConfigureAwait(false); - connection.Value.MqttConnection.Dispose(); + await mqttConnection.StopAsync().ConfigureAwait(false); + mqttConnection.Dispose(); } _mqttConnections.Clear(); } - private class MqttConnectionEntry + private sealed class MqttConnectionEntry { public MqttConnectionEntry(MqttConnection mqttConnection) { diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/IMqttConnection.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/IMqttConnection.cs index adb8cb2..5a6e41e 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/IMqttConnection.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/IMqttConnection.cs @@ -14,7 +14,7 @@ public interface IMqttConnection : IDisposable Task StopAsync(); - Task SubscribeAsync(TopicFilter[] topics); + Task SubscribeAsync(MqttTopicFilter[] topics); Task UnubscribeAsync(string[] topics); } diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttConnection.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttConnection.cs index 8fd30fe..2074998 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttConnection.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttConnection.cs @@ -112,8 +112,8 @@ public Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs) { throw new ArgumentNullException(nameof(eventArgs)); } - - if (eventArgs.AuthenticateResult.ResultCode == MqttClientConnectResultCode.Success) + + if (eventArgs.ConnectResult.ResultCode == MqttClientConnectResultCode.Success) { ConnectionState = ConnectionState.Connected; _logger.LogInformation($"MqttConnection Connected for {this}"); @@ -121,7 +121,7 @@ public Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs) else { ConnectionState = ConnectionState.Disconnected; - _logger.LogWarning($"MqttConnection could not connect, result code: {eventArgs.AuthenticateResult.ResultCode} for {this}"); + _logger.LogWarning($"MqttConnection could not connect, result code: {eventArgs.ConnectResult.ResultCode} for {this}"); } return Task.CompletedTask; } @@ -164,7 +164,7 @@ public async Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageRe /// Subscribe to one or more topics. /// /// The topics to subscribe to. - public async Task SubscribeAsync(TopicFilter[] topics) + public async Task SubscribeAsync(MqttTopicFilter[] topics) { if (_managedMqttClient == null) { diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttListener.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttListener.cs index a22d743..e192be3 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttListener.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/Listeners/MqttListener.cs @@ -20,7 +20,7 @@ public sealed class MqttListener : IListener, IProcesMqttMessage private readonly ILogger _logger; private readonly CancellationTokenSource _cancellationTokenSource; private readonly IMqttConnection _mqttConnection; - private readonly TopicFilter[] _topics; + private readonly MqttTopicFilter[] _topics; private bool _disposed; /// @@ -30,7 +30,7 @@ public sealed class MqttListener : IListener, IProcesMqttMessage /// The topics to subscribe to. /// Allows the function to be executed. /// The logger. - public MqttListener(IMqttConnection connection, TopicFilter[] topics, ITriggeredFunctionExecutor executor, ILogger logger) + public MqttListener(IMqttConnection connection, MqttTopicFilter[] topics, ITriggeredFunctionExecutor executor, ILogger logger) { _executor = executor; _logger = logger; diff --git a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs index c503f42..68d7e3f 100644 --- a/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs +++ b/src/CaseOnline.Azure.WebJobs.Extensions.Mqtt/MqttTriggerAttribute.cs @@ -20,15 +20,14 @@ public MqttTriggerAttribute(params string[] topics) public MqttTriggerAttribute(string topic, Messaging.MqttQualityOfServiceLevel qos = Messaging.MqttQualityOfServiceLevel.AtLeastOnce, Messaging.NoLocal noLocal = Messaging.NoLocal.NotSet, Messaging.RetainAsPublished retainAsPublished = Messaging.RetainAsPublished.NotSet, Messaging.MqttRetainHandling retainHandling = Messaging.MqttRetainHandling.NotSet) { - TopicFilter = new TopicFilter + TopicFilter = new MqttTopicFilter { Topic = topic, NoLocal = noLocal switch { Messaging.NoLocal.True => true, Messaging.NoLocal.False => false, - Messaging.NoLocal.NotSet => null, - _ => null + _ => false }, QualityOfServiceLevel = qos switch { @@ -41,16 +40,14 @@ public MqttTriggerAttribute(string topic, Messaging.MqttQualityOfServiceLevel qo { Messaging.RetainAsPublished.True => true, Messaging.RetainAsPublished.False => false, - Messaging.RetainAsPublished.NotSet => null, - _ => null + _ => false }, RetainHandling = retainHandling switch { Messaging.MqttRetainHandling.DoNotSendOnSubscribe => MqttRetainHandling.DoNotSendOnSubscribe, Messaging.MqttRetainHandling.SendAtSubscribe => MqttRetainHandling.SendAtSubscribe, Messaging.MqttRetainHandling.SendAtSubscribeIfNewSubscriptionOnly => MqttRetainHandling.SendAtSubscribeIfNewSubscriptionOnly, - Messaging.MqttRetainHandling.NotSet => null, - _ => null, + _ => MqttRetainHandling.DoNotSendOnSubscribe, } }; } @@ -60,7 +57,7 @@ public MqttTriggerAttribute(Type mqttConfigCreatorType, params string[] topics) TopicStrings = topics; } - public TopicFilter TopicFilter { get; } + public MqttTopicFilter TopicFilter { get; } internal string[] TopicStrings { get; } } diff --git a/src/ExampleFunctions/ExampleFunctions.csproj b/src/ExampleFunctions/ExampleFunctions.csproj index c0c0d10..d185eb4 100644 --- a/src/ExampleFunctions/ExampleFunctions.csproj +++ b/src/ExampleFunctions/ExampleFunctions.csproj @@ -1,13 +1,15 @@  - netcoreapp3.0 - v2 + net5.0 + v3 latest {12D92D5F-E9FA-4816-9271-B07545DD0840} - - + + + + diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj index e41b714..476d38a 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.csproj @@ -1,6 +1,6 @@  - netcoreapp3.0 + net5.0 false Full {DC968091-3FBF-4B78-A60F-E4818DF0022E} @@ -54,16 +54,22 @@ - - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + - + all runtime; build; native; contentfiles; analyzers - + diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/TriggerTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/TriggerTests.cs index 4c32452..b5005ff 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/TriggerTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/EndToEnd/TriggerTests.cs @@ -145,7 +145,7 @@ public static void Testert([MqttTrigger("test/topic", ConnectionString = "MqttCo [Fact] public async Task WhenTlsIsSetToTrueASecureConnectionIsMade() { - var serializedServerCertificate = new X509Certificate(@"Certificates/myRootCA.pfx", "12345", X509KeyStorageFlags.Exportable) + var serializedServerCertificate = new X509Certificate2(@"Certificates/myRootCA.pfx", "12345", X509KeyStorageFlags.Exportable) .Export(X509ContentType.Pfx); var options = new MqttServerOptionsBuilder() diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/JobHostHelper.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/JobHostHelper.cs index d377d7b..60ab329 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/JobHostHelper.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/JobHostHelper.cs @@ -71,7 +71,7 @@ public async Task WaitForAllConnectionToBeConnected() if (mqttExtensionConfigProvider.AllConnectionsConnected()) { Debug.WriteLine($"JobHost boot, waited for {i * sleepDuration.TotalMilliseconds}ms to be connected"); - await Task.Delay(sleepDuration); //wait for another second to realy become connected + await Task.Delay(sleepDuration); //wait for another second to really become connected break; } await Task.Delay(sleepDuration); diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttClientHelper.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttClientHelper.cs index ed50125..208ce7a 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttClientHelper.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttClientHelper.cs @@ -97,7 +97,7 @@ public void Dispose() public async Task SubscribeAsync(string topic) { - await _mqttClient.SubscribeAsync(new List() { new TopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }); + await _mqttClient.SubscribeAsync(new List() { new MqttTopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }); await Task.Delay(TimeSpan.FromSeconds(1)); } diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttLogger.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttLogger.cs index 28b7012..0f239d0 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttLogger.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttLogger.cs @@ -1,6 +1,7 @@ using System; using Microsoft.Extensions.Logging; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests.Helpers { @@ -20,7 +21,7 @@ public MqttLogger(ILogger logger) private void MqttLogger_LogMessagePublished(object sender, MqttNetLogMessagePublishedEventArgs e) { - Logger.LogTrace($"{e.TraceMessage.Level}:{e.TraceMessage.Message}"); + Logger.LogTrace($"{e.LogMessage.Level}:{e.LogMessage.Message}"); } public void Error(Exception exception, string message, params object[] parameters) @@ -58,9 +59,9 @@ public void Verbose(string message, params object[] parameters) Logger.LogTrace(message, parameters); } - public IMqttNetChildLogger CreateChildLogger(string source = null) + public IMqttNetLogger CreateChildLogger(string source = null) { - return new MqttNetChildLogger(this, source); + return new MqttNetEventLogger(source); } public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) @@ -81,7 +82,14 @@ public void Publish(MqttNetLogLevel logLevel, string source, string message, obj loggerLogLevel = LogLevel.Warning; break; } - Logger.Log(loggerLogLevel, new EventId(), message, exception, (x,y) => $"{string.Format(x, parameters)}: {y?.Message}"); + if (parameters is not null && parameters.Length > 0) + { + Logger.Log(loggerLogLevel, new EventId(), message, exception, (x, y) => $"{string.Format(x, parameters)}: {y?.Message}"); + } + else + { + Logger.Log(loggerLogLevel, new EventId(), message, exception, (x, y) => $"{string.Format(x)}: {y?.Message}"); + } } } } diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttServerHelper.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttServerHelper.cs index 5f1f205..2c84105 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttServerHelper.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/Helpers/MqttServerHelper.cs @@ -98,7 +98,7 @@ public async Task PublishAsync(IEnumerable applicationMe public async Task SubscribeAsync(string topic) { - await _mqttServer.SubscribeAsync("Custom", new List() { new TopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }); + await _mqttServer.SubscribeAsync("Custom", new List() { new MqttTopicFilter() { Topic = topic, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }); } public async Task PublishAsync(MqttApplicationMessage applicationMessage) diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttConnectionTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttConnectionTests.cs index 42c25e2..b8cb372 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttConnectionTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttConnectionTests.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Config; @@ -47,10 +48,8 @@ public async Task StartAsyncSubscribesToTopics() // Act await mqttConnection.StartAsync(messageProcessor.Object); - await mqttConnection.HandleConnectedAsync(new MqttClientConnectedEventArgs(new MqttClientAuthenticateResult { - IsSessionPresent = true, - ResultCode = MqttClientConnectResultCode.Success - })); + var mqttClientConnectResult = new MqttClientConnectResult(); + await mqttConnection.HandleConnectedAsync(new MqttClientConnectedEventArgs(new MqttClientConnectResult())); // Assert Assert.Equal(ConnectionState.Connected, mqttConnection.ConnectionState); @@ -75,10 +74,11 @@ public async Task NewMessageIsProcessed() var config = new MqttConfiguration("CustomConfig", mockManagedMqttClientOptions.Object); var mqttConnection = new MqttConnection(mockMqttClientFactory.Object, config, _mockLogger.Object); - + Func acknowledgeHandler = (MqttApplicationMessageReceivedEventArgs eventArgs, CancellationToken cancellationToken) => Task.CompletedTask; + // Act await mqttConnection.StartAsync(messageProcessor.Object); - await mqttConnection.HandleApplicationMessageReceivedAsync( new MqttApplicationMessageReceivedEventArgs("ClientId", DefaultMessage)); + await mqttConnection.HandleApplicationMessageReceivedAsync(new MqttApplicationMessageReceivedEventArgs("ClientId", DefaultMessage, new MQTTnet.Packets.MqttPublishPacket(), acknowledgeHandler)); // Assert messageProcessor.Verify(x => x.OnMessage(It.Is(y => y.Message.Topic == DefaultMessage.Topic))); diff --git a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttListenerTests.cs b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttListenerTests.cs index acae1d7..198e410 100644 --- a/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttListenerTests.cs +++ b/test/CaseOnline.Azure.WebJobs.Extensions.Mqtt.Tests/MqttListenerTests.cs @@ -25,14 +25,14 @@ public async Task StartAsyncSubscribesToTopics() var mockTriggeredFunctionExecutor = new Mock(); mockMqttConnection - .Setup(x => x.SubscribeAsync(It.IsAny())) + .Setup(x => x.SubscribeAsync(It.IsAny())) .Returns(Task.CompletedTask); mockTriggeredFunctionExecutor .Setup(x => x.TryExecuteAsync(It.IsAny(), It.IsAny())); - var topicFilter = new TopicFilter[] { new TopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; - var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); + var MqttTopicFilter = new MqttTopicFilter[] { new MqttTopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; + var mqttListener = new MqttListener(mockMqttConnection.Object, MqttTopicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); // Act await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false); @@ -51,15 +51,15 @@ public async Task StopAsyncUnsubscribesToTopics() var mockTriggeredFunctionExecutor = new Mock(); mockMqttConnection - .Setup(x => x.SubscribeAsync(It.IsAny())) + .Setup(x => x.SubscribeAsync(It.IsAny())) .Returns(Task.CompletedTask); mockMqttConnection .Setup(x => x.UnubscribeAsync(It.IsAny())) .Returns(Task.CompletedTask); - var topicFilter = new TopicFilter[] { new TopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; - var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); + var MqttTopicFilter = new MqttTopicFilter[] { new MqttTopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; + var mqttListener = new MqttListener(mockMqttConnection.Object, MqttTopicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); // Act await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false); @@ -77,14 +77,14 @@ public async Task MessageForOtherTopicThanSubscribedToIsProcessed() var mockTriggeredFunctionExecutor = new Mock(); mockMqttConnection - .Setup(x => x.SubscribeAsync(It.IsAny())) + .Setup(x => x.SubscribeAsync(It.IsAny())) .Returns(Task.CompletedTask); mockTriggeredFunctionExecutor .Setup(x => x.TryExecuteAsync(It.IsAny(), It.IsAny())); - var topicFilter = new TopicFilter[] { new TopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; - var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); + var MqttTopicFilter = new MqttTopicFilter[] { new MqttTopicFilter() { Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce } }; + var mqttListener = new MqttListener(mockMqttConnection.Object, MqttTopicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object); // Act await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);