Skip to content

Commit 2c8ba6d

Browse files
committed
升级为了.net9
1 parent 4649da9 commit 2c8ba6d

File tree

6 files changed

+27
-64
lines changed

6 files changed

+27
-64
lines changed

ExampleClient/ExampleClient.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.2.930" />
9+
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

ExampleClient/Program.cs

+12-41
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using MQTTnet;
2-
using MQTTnet.Client;
3-
using MQTTnet.Extensions.ManagedClient;
4-
using MQTTnet.Packets;
52
using System;
6-
using System.Collections.Generic;
73

84
namespace ExampleClient
95
{
@@ -12,56 +8,31 @@ internal class Program
128
private static async System.Threading.Tasks.Task Main(string[] args)
139
{
1410
var rnd = new Random();
15-
// Setup and start a managed MQTT client.
16-
var options = new ManagedMqttClientOptionsBuilder()
17-
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
18-
.WithClientOptions(new MqttClientOptionsBuilder()
11+
var options = new MqttClientOptionsBuilder ()
1912
.WithClientId($"Client{rnd.Next(0, 1000)}")
20-
.WithWebSocketServer( cfg=> cfg.WithUri("localhost:50482/mqtt"))
21-
.Build())
22-
.Build();
23-
24-
var mqttClient = new MqttFactory().CreateManagedMqttClient();
25-
await mqttClient.SubscribeAsync(new List<MqttTopicFilter> { new MqttTopicFilterBuilder().WithTopic("MqttWeatherForecast/90210/temperature").Build() });
13+
.WithWebSocketServer( cfg=> cfg.WithUri("ws://localhost:5000/mqtt"))
14+
.Build();
2615

16+
var mqttClient = new MqttClientFactory().CreateMqttClient();
2717
mqttClient.ConnectedAsync += (e) =>
2818
{
29-
Console.WriteLine($"Connection Result: {e.ConnectResult.ResultCode}");
19+
Console.WriteLine($"Connected : {e.ConnectResult.ResultCode}");
3020
return System.Threading.Tasks.Task.CompletedTask;
3121
};
32-
33-
mqttClient.ConnectingFailedAsync += (e) =>
22+
mqttClient.DisconnectedAsync += (e) =>
3423
{
35-
Console.WriteLine($"Connection Failed: {e.Exception}");
24+
Console.WriteLine($"Disconnected : {e.Exception}");
3625
return System.Threading.Tasks.Task.CompletedTask;
3726
};
38-
3927
mqttClient.ApplicationMessageReceivedAsync += e =>
4028
{
41-
Console.WriteLine($"Message from {e.ClientId}: {e.ApplicationMessage.PayloadSegment.Count } bytes.");
29+
Console.WriteLine($"Message from {e.ClientId}: {e.ApplicationMessage.Payload.Length } bytes.");
4230
return System.Threading.Tasks.Task.CompletedTask;
4331
};
44-
45-
await mqttClient.StartAsync(options);
46-
47-
// Publish a message on a well known topic
48-
await mqttClient.EnqueueAsync(new ManagedMqttApplicationMessageBuilder().WithApplicationMessage(msg =>
49-
{
50-
msg.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce);
51-
msg.WithPayload(BitConverter.GetBytes(98.6d));
52-
msg.WithTopic("MqttWeatherForecast/90210/temperature");
53-
}).Build());
54-
55-
// Publish a message on a topic the server doesn't explicitly handle
56-
await mqttClient.EnqueueAsync(new ManagedMqttApplicationMessageBuilder().WithApplicationMessage(msg =>
57-
{
58-
msg.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce);
59-
msg.WithPayload(BitConverter.GetBytes(100d));
60-
msg.WithTopic("asdfsdfsadfasdf");
61-
}).Build());
62-
63-
// StartAsync returns immediately, as it starts a new thread using Task.Run, and so the calling thread needs
64-
// to wait.
32+
await mqttClient.ConnectAsync(options);
33+
await mqttClient.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("MqttWeatherForecast/90210/temperature").Build());
34+
await mqttClient.PublishAsync(new MqttApplicationMessageBuilder().WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce).WithPayload(BitConverter.GetBytes(98.6d)).WithTopic("MqttWeatherForecast/90210/temperature").Build());
35+
await mqttClient.PublishAsync(new MqttApplicationMessageBuilder().WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce).WithPayload(BitConverter.GetBytes(100d)).WithTopic("asdfsdfsadfasdf").Build());
6536
Console.ReadLine();
6637
}
6738
}

ExampleServer/ExampleServer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

Source/MQTTnet.AspNetCore.Routing.csproj

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
54
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
65
<Description>
76
This is a support library to integrate AttributeRouting into MQTTnet with AspNetCore.
@@ -26,21 +25,18 @@
2625
<IncludeSymbols>true</IncludeSymbols>
2726
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2827
<EnableNETAnalyzers>true</EnableNETAnalyzers>
28+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
2929

3030
</PropertyGroup>
3131
<ItemGroup>
3232
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
3333
<_Parameter1>MQTTnet.AspNetCore.Routing.Tests</_Parameter1>
3434
</AssemblyAttribute>
3535
</ItemGroup>
36-
3736

38-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
39-
<FrameworkReference Include="Microsoft.AspNetCore.App" />
40-
</ItemGroup>
4137

4238
<ItemGroup>
43-
<PackageReference Include="MQTTnet" Version="4.3.2.930" />
44-
<PackageReference Include="MQTTnet.AspNetCore" Version="4.3.2.930" />
39+
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
40+
<PackageReference Include="MQTTnet.AspNetCore" Version="5.0.1.1416" />
4541
</ItemGroup>
4642
</Project>

Source/Routing/MqttRouter.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using MQTTnet.AspNetCore.Routing.Attributes;
66
using MQTTnet.Server;
77
using System;
8+
using System.Buffers;
89
using System.Collections.Generic;
910
using System.Linq;
1011
using System.Reflection;
@@ -186,13 +187,13 @@ private static Task HandlerInvoker(MethodInfo method, object instance, object?[]
186187
{
187188
JsonSerializerOptions? defaultOptions =
188189
serviceProvider.GetService<MqttRoutingOptions>()?.SerializerOptions;
189-
return JsonSerializer.Deserialize(controllerContext.MqttContext.ApplicationMessage.Payload,
190+
return JsonSerializer.Deserialize(new ReadOnlySpan<byte>(controllerContext.MqttContext.ApplicationMessage.Payload.ToArray()),
190191
param.ParameterType,
191192
defaultOptions
192193
);
193194
}
194-
195-
if (!availableParmeters.TryGetValue(param.Name, out object? value))
195+
object? value = null;
196+
if (param?.Name!=null && !availableParmeters.TryGetValue(param.Name, out value))
196197
{
197198
if (param.IsOptional)
198199
{
@@ -204,7 +205,7 @@ private static Task HandlerInvoker(MethodInfo method, object instance, object?[]
204205
}
205206
}
206207

207-
if (!param.ParameterType.IsAssignableFrom(value.GetType()))
208+
if (value !=null && param!=null && !param.ParameterType.IsAssignableFrom(value.GetType()))
208209
{
209210
try
210211
{

Tests/MQTTnet.AspNetCore.Routing.Tests/MQTTnet.AspNetCore.Routing.Tests.csproj

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0</TargetFrameworks>
5-
6-
<IsPackable>false</IsPackable>
7-
<AssemblyName>MQTTnet.AspNetCore.Routing.Tests</AssemblyName>
8-
9-
<RootNamespace>MQTTnet.AspNetCore.Routing.Tests</RootNamespace>
4+
<TargetFramework>net9.0</TargetFramework>
105
</PropertyGroup>
116

127
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
14-
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
15-
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
9+
<PackageReference Include="MSTest.TestAdapter" Version="3.8.2" />
10+
<PackageReference Include="MSTest.TestFramework" Version="3.8.2" />
1611
</ItemGroup>
1712
<ItemGroup>
1813
<ProjectReference Include="..\..\Source\MQTTnet.AspNetCore.Routing.csproj" />

0 commit comments

Comments
 (0)