mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
fixed core 3.1 startup and verified it still works with 2.1 and net461
This commit is contained in:
@@ -40,8 +40,11 @@ namespace MQTTnet.AspNetCore.Client.Tcp
|
|||||||
_sender = new SocketSender(_socket, PipeScheduler.ThreadPool);
|
_sender = new SocketSender(_socket, PipeScheduler.ThreadPool);
|
||||||
_receiver = new SocketReceiver(_socket, PipeScheduler.ThreadPool);
|
_receiver = new SocketReceiver(_socket, PipeScheduler.ThreadPool);
|
||||||
}
|
}
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
public override ValueTask DisposeAsync()
|
||||||
|
#else
|
||||||
public Task DisposeAsync()
|
public Task DisposeAsync()
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
|
|
||||||
@@ -50,8 +53,15 @@ namespace MQTTnet.AspNetCore.Client.Tcp
|
|||||||
|
|
||||||
_socket?.Dispose();
|
_socket?.Dispose();
|
||||||
|
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
|
||||||
|
return base.DisposeAsync();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
public async Task StartAsync()
|
public async Task StartAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
+2
-11
@@ -1,14 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Linq;
|
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace MQTTnet.AspNetCore
|
namespace MQTTnet.AspNetCore
|
||||||
{
|
{
|
||||||
public static class ApplicationBuilderExtensions
|
public static class ApplicationBuilderExtensions
|
||||||
{
|
{
|
||||||
|
[Obsolete("This class is obsolete and will be removed in a future version. The recommended alternative is to use MapMqtt inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
|
||||||
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
|
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
|
||||||
{
|
{
|
||||||
app.UseWebSockets();
|
app.UseWebSockets();
|
||||||
@@ -24,7 +23,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
if (context.Request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
|
if (context.Request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
|
||||||
{
|
{
|
||||||
subProtocol = SelectSubProtocol(requestedSubProtocolValues);
|
subProtocol = MqttSubProtocolSelector.SelectSubProtocol(requestedSubProtocolValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
|
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
|
||||||
@@ -37,14 +36,6 @@ namespace MQTTnet.AspNetCore
|
|||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string SelectSubProtocol(IList<string> requestedSubProtocolValues)
|
|
||||||
{
|
|
||||||
// Order the protocols to also match "mqtt", "mqttv-3.1", "mqttv-3.11" etc.
|
|
||||||
return requestedSubProtocolValues
|
|
||||||
.OrderByDescending(p => p.Length)
|
|
||||||
.FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<IMqttServer> configure)
|
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<IMqttServer> configure)
|
||||||
{
|
{
|
||||||
var server = app.ApplicationServices.GetRequiredService<IMqttServer>();
|
var server = app.ApplicationServices.GetRequiredService<IMqttServer>();
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Http.Connections;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MQTTnet.AspNetCore
|
||||||
|
{
|
||||||
|
public static class ConnectionRouteBuilderExtensions
|
||||||
|
{
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
[Obsolete("This class is obsolete and will be removed in a future version. The recommended alternative is to use MapMqtt inside Microsoft.AspNetCore.Builder.UseEndpoints(...).")]
|
||||||
|
#endif
|
||||||
|
public static void MapMqtt(this ConnectionsRouteBuilder connection, PathString path)
|
||||||
|
{
|
||||||
|
connection.MapConnectionHandler<MqttConnectionHandler>(path, options =>
|
||||||
|
{
|
||||||
|
options.WebSockets.SubProtocolSelector = MqttSubProtocolSelector.SelectSubProtocol;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
|
||||||
|
namespace MQTTnet.AspNetCore
|
||||||
|
{
|
||||||
|
public static class EndpointRouterExtensions
|
||||||
|
{
|
||||||
|
public static void MapMqtt(this IEndpointRouteBuilder endpoints, string pattern)
|
||||||
|
{
|
||||||
|
endpoints.MapConnectionHandler<MqttConnectionHandler>(pattern, options =>
|
||||||
|
{
|
||||||
|
options.WebSockets.SubProtocolSelector = MqttSubProtocolSelector.SelectSubProtocol;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
+1
-1
@@ -1,10 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using MQTTnet.Adapter;
|
using MQTTnet.Adapter;
|
||||||
using MQTTnet.Diagnostics;
|
using MQTTnet.Diagnostics;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
using MQTTnet.Implementations;
|
using MQTTnet.Implementations;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace MQTTnet.AspNetCore
|
namespace MQTTnet.AspNetCore
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,8 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return StartAsync(_options);
|
_ = StartAsync(_options);
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MQTTnet.AspNetCore
|
||||||
|
{
|
||||||
|
public static class MqttSubProtocolSelector
|
||||||
|
{
|
||||||
|
public static string SelectSubProtocol(IList<string> requestedSubProtocolValues)
|
||||||
|
{
|
||||||
|
// Order the protocols to also match "mqtt", "mqttv-3.1", "mqttv-3.11" etc.
|
||||||
|
return requestedSubProtocolValues
|
||||||
|
.OrderByDescending(p => p.Length)
|
||||||
|
.FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
|
<ProjectReference Include="..\..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
|
||||||
<ProjectReference Include="..\..\Source\MQTTnet\MQTTnet.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -26,11 +26,8 @@ namespace MQTTnet.Benchmarks
|
|||||||
_host = WebHost.CreateDefaultBuilder()
|
_host = WebHost.CreateDefaultBuilder()
|
||||||
.UseKestrel(o => o.ListenAnyIP(1883, l => l.UseMqtt()))
|
.UseKestrel(o => o.ListenAnyIP(1883, l => l.UseMqtt()))
|
||||||
.ConfigureServices(services => {
|
.ConfigureServices(services => {
|
||||||
var mqttServerOptions = new MqttServerOptionsBuilder()
|
|
||||||
.WithoutDefaultEndpoint()
|
|
||||||
.Build();
|
|
||||||
services
|
services
|
||||||
.AddHostedMqttServer(mqttServerOptions)
|
.AddHostedMqttServer(mqttServerOptions => mqttServerOptions.WithoutDefaultEndpoint())
|
||||||
.AddMqttConnectionHandler();
|
.AddMqttConnectionHandler();
|
||||||
})
|
})
|
||||||
.Configure(app => {
|
.Configure(app => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFrameworks>net461;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
|
||||||
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
|
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using MQTTnet.AspNetCore;
|
using MQTTnet.AspNetCore;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace MQTTnet.TestApp.AspNetCore2
|
namespace MQTTnet.TestApp.AspNetCore2
|
||||||
{
|
{
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static Task Main(string[] args)
|
||||||
{
|
{
|
||||||
BuildWebHost(args).Run();
|
return BuildWebHost(args).RunAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IWebHost BuildWebHost(string[] args) =>
|
private static IWebHost BuildWebHost(string[] args) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
WebHost.CreateDefaultBuilder(args)
|
||||||
.UseKestrel(o => {
|
.UseKestrel(o =>
|
||||||
|
{
|
||||||
o.ListenAnyIP(1883, l => l.UseMqtt());
|
o.ListenAnyIP(1883, l => l.UseMqtt());
|
||||||
o.ListenAnyIP(5000); // default http pipeline
|
o.ListenAnyIP(5000); // default http pipeline
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.Versioning;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using MQTTnet.AspNetCore;
|
using MQTTnet.AspNetCore;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Server;
|
||||||
|
|
||||||
@@ -16,29 +19,37 @@ namespace MQTTnet.TestApp.AspNetCore2
|
|||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
var mqttServerOptions = new MqttServerOptionsBuilder()
|
|
||||||
.WithoutDefaultEndpoint()
|
|
||||||
.Build();
|
|
||||||
services
|
services
|
||||||
.AddHostedMqttServer(mqttServerOptions)
|
.AddHostedMqttServer(mqttServer => mqttServer.WithoutDefaultEndpoint())
|
||||||
.AddMqttConnectionHandler()
|
.AddMqttConnectionHandler()
|
||||||
.AddConnections();
|
.AddConnections();
|
||||||
}
|
}
|
||||||
|
|
||||||
// In class _Startup_ of the ASP.NET Core 2.0 project.
|
// In class _Startup_ of the ASP.NET Core 3.1 project.
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapMqtt("/mqtt");
|
||||||
|
});
|
||||||
|
#else
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
app.UseConnections(c => c.MapConnectionHandler<MqttConnectionHandler>("/mqtt", options => {
|
app.UseConnections(c => c.MapMqtt("/mqtt"));
|
||||||
options.WebSockets.SubProtocolSelector = MQTTnet.AspNetCore.ApplicationBuilderExtensions.SelectSubProtocol;
|
#endif
|
||||||
}));
|
|
||||||
|
|
||||||
//app.UseMqttEndpoint();
|
|
||||||
app.UseMqttServer(server =>
|
app.UseMqttServer(server =>
|
||||||
{
|
{
|
||||||
server.StartedHandler = new MqttServerStartedHandlerDelegate(async args =>
|
server.StartedHandler = new MqttServerStartedHandlerDelegate(async args =>
|
||||||
{
|
{
|
||||||
|
var frameworkName = GetType().Assembly.GetCustomAttribute<TargetFrameworkAttribute>()?
|
||||||
|
.FrameworkName;
|
||||||
|
|
||||||
var msg = new MqttApplicationMessageBuilder()
|
var msg = new MqttApplicationMessageBuilder()
|
||||||
.WithPayload("Mqtt is awesome")
|
.WithPayload($"Mqtt hosted on {frameworkName} is awesome")
|
||||||
.WithTopic("message");
|
.WithTopic("message");
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@@ -46,7 +57,7 @@ namespace MQTTnet.TestApp.AspNetCore2
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await server.PublishAsync(msg.Build());
|
await server.PublishAsync(msg.Build());
|
||||||
msg.WithPayload("Mqtt is still awesome at " + DateTime.Now);
|
msg.WithPayload($"Mqtt hosted on {frameworkName} is still awesome at {DateTime.Now}");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user