Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade MQTTNet to latest version, add support for .NET 5 #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 41 additions & 41 deletions CaseOnline.Azure.WebJobs.Extensions.Mqtt.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
64 changes: 64 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -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)'
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ private IMqttClientOptions GetMqttClientOptions(MqttConnectionString connectionS

if (connectionString.Tls)
{
var certificates = new List<byte[]>();
var certificates = new List<X509Certificate2>();
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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static MqttTriggerAttribute GetMqttTriggerAttribute(ParameterInfo parame

private ITriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute)
{
var topicFilters = new List<TopicFilter>();
var topicFilters = new List<MqttTopicFilter>();

var mqttConnection = _connectionFactory.GetMqttConnection(mqttTriggerAttribute);
try
Expand All @@ -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 };
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> _emptyBindingData = new Dictionary<string, object>();

Expand All @@ -31,7 +31,7 @@ public class MqttTriggerBinding : ITriggerBinding
/// <param name="connection">The MQTT connection.</param>
/// <param name="topics">The topics to subscribe to.</param>
/// <param name="logger">The logger.</param>
public MqttTriggerBinding(ParameterInfo parameter, MqttConnection connection, TopicFilter[] topics, ILogger logger)
public MqttTriggerBinding(ParameterInfo parameter, MqttConnection connection, MqttTopicFilter[] topics, ILogger logger)
{
_parameter = parameter;
_connection = connection;
Expand Down Expand Up @@ -82,15 +82,15 @@ public ParameterDescriptor ToParameterDescriptor()
return descriptor;
}

private class MqttTriggerParameterDescriptor : TriggerParameterDescriptor
private sealed class MqttTriggerParameterDescriptor : TriggerParameterDescriptor
{
public override string GetTriggerReason(IDictionary<string, string> arguments)
{
return string.Format("Mqtt fired at {0}", DateTime.Now.ToString("o"));
}
}

private class ValueProvider : IValueProvider
private sealed class ValueProvider : IValueProvider
{
private readonly object _value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<Authors>Kees Schollaart</Authors>
<Company>Case Online</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down Expand Up @@ -45,23 +45,20 @@ For more information, please visit https://github.com/keesschollaart81/CaseOnlin
<AdditionalFiles Include="stylecop.json" Link="stylecop.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.5.0.15942">
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.33.0.40503">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.6" />
<PackageReference Include="MQTTnet" Version="3.0.8" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="MQTTnet" Version="3.1.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<None Update="logo.png" Pack="true" PackagePath="">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IMqttConnection : IDisposable

Task StopAsync();

Task SubscribeAsync(TopicFilter[] topics);
Task SubscribeAsync(MqttTopicFilter[] topics);

Task UnubscribeAsync(string[] topics);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ 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}");
}
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;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public async Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageRe
/// Subscribe to one or more topics.
/// </summary>
/// <param name="topics">The topics to subscribe to.</param>
public async Task SubscribeAsync(TopicFilter[] topics)
public async Task SubscribeAsync(MqttTopicFilter[] topics)
{
if (_managedMqttClient == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -30,7 +30,7 @@ public sealed class MqttListener : IListener, IProcesMqttMessage
/// <param name="topics">The topics to subscribe to.</param>
/// <param name="executor">Allows the function to be executed.</param>
/// <param name="logger">The logger.</param>
public MqttListener(IMqttConnection connection, TopicFilter[] topics, ITriggeredFunctionExecutor executor, ILogger logger)
public MqttListener(IMqttConnection connection, MqttTopicFilter[] topics, ITriggeredFunctionExecutor executor, ILogger logger)
{
_executor = executor;
_logger = logger;
Expand Down
Loading