Skip to content

Commit fcb9aeb

Browse files
committed
处理值转换问题
1 parent 3f45daf commit fcb9aeb

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

ExampleServer/MqttControllers/MqttWeatherForecastController.cs

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Logging;
2+
using MQTTnet;
23
using MQTTnet.AspNetCore.AttributeRouting;
34
using System;
45
using System.Threading.Tasks;
@@ -18,24 +19,27 @@ public MqttWeatherForecastController(ILogger<MqttWeatherForecastController> logg
1819
}
1920

2021
// Supports template routing with typed constraints
21-
[MqttRoute("{zipCode:int}/temperature")]
22+
[MqttRoute("{zipCode}/temperature")]
2223
public Task WeatherReport(int zipCode)
2324
{
2425
// We have access to the MqttContext
2526
if (zipCode != 90210) { MqttContext.CloseConnection = true; }
2627

2728
// We have access to the raw message
28-
var temperature = BitConverter.ToDouble(Message.Payload);
29-
30-
_logger.LogInformation($"It's {temperature} degrees in Hollywood");
31-
32-
// Example validation
33-
if (temperature <= 0 || temperature >= 130)
29+
if (int.TryParse(Message.ConvertPayloadToString(), out int temperature))
3430
{
35-
return BadMessage();
31+
_logger.LogInformation($"It's {temperature} degrees in Hollywood");
32+
// Example validation
33+
if (temperature <= 0 || temperature >= 130)
34+
{
35+
return BadMessage();
36+
}
37+
else
38+
{
39+
return Ok();
40+
}
3641
}
37-
38-
return Ok();
42+
return BadMessage();
3943
}
4044
}
4145
}

ExampleServer/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2424
webBuilder.UseKestrel(
2525
o =>
2626
{
27-
o.ListenAnyIP(50483, l => l.UseMqtt()); // MQTT pipeline
28-
o.ListenAnyIP(50482); // Default HTTP pipeline
27+
o.ListenAnyIP(1883, l => l.UseMqtt()); // MQTT pipeline
28+
o.ListenAnyIP(5000); // Default HTTP pipeline
2929
});
3030
webBuilder.ConfigureLogging(opts => opts.AddConsole());
3131
webBuilder.UseStartup<Startup>();
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Description>
7+
This is a support library to integrate AttributeRouting into MQTTnet with AspNetCore.
8+
9+
Easily create Controllers and Actions to process incoming MQTT messages using attribute-based routing against the incoming message topic.
10+
</Description>
11+
<Copyright>Copyright (c) Atlas Lift Tech Inc. 2021</Copyright>
12+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13+
<PackageTags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor AspNetCore</PackageTags>
14+
<Company>Atlas Lift Tech Inc.</Company>
15+
<Authors>Anton Vishnyak</Authors>
16+
<AssemblyVersion>0.3.16</AssemblyVersion>
17+
<FileVersion>0.3.16</FileVersion>
18+
<LangVersion>default</LangVersion>
19+
<RepositoryUrl>https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting</RepositoryUrl>
20+
<RepositoryType>GIT</RepositoryType>
21+
<PackageReleaseNotes>* Added support for passing an array of assemblies to use in route discovery</PackageReleaseNotes>
22+
<Version>0.3.16</Version>
23+
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
24+
<PackageProjectUrl>https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting</PackageProjectUrl>
25+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
26+
<IncludeSymbols>true</IncludeSymbols>
27+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
28+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
29+
</PropertyGroup>
30+
31+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
32+
<PackageReference Include="Microsoft.AspNetCore.Http.Connections" Version="1.1.0" />
33+
</ItemGroup>
34+
35+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
36+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
41+
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
42+
<PackageReference Include="MQTTnet" Version="4.0.1.184" />
43+
<PackageReference Include="MQTTnet.AspNetCore" Version="4.0.1.184" />
44+
</ItemGroup>
45+
</Project>

Source/Routing/MqttRouter.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ internal async Task OnIncomingApplicationMessage(IServiceProvider svcProvider, I
132132
}
133133
}
134134
}
135+
135136
}
136137

137138
private static Task HandlerInvoker(MethodInfo method, object instance, object?[]? parameters)
@@ -173,9 +174,15 @@ private static Task HandlerInvoker(MethodInfo method, object instance, object?[]
173174

174175
if (!param.ParameterType.IsAssignableFrom(value.GetType()))
175176
{
176-
throw new ArgumentException($"Cannot assign type \"{value.GetType()}\" to parameter \"{param.ParameterType.Name} {param.Name}\"", param.Name);
177+
try
178+
{
179+
value = Convert.ChangeType(value, param.ParameterType);
180+
}
181+
catch (Exception ex)
182+
{
183+
throw new ArgumentException($"Cannot assign type \"{value.GetType()}\" to parameter \"{param.ParameterType.Name} {param.Name}\"", param.Name,ex);
184+
}
177185
}
178-
179186
return value;
180187
}
181188
}

0 commit comments

Comments
 (0)