-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support for hosting extensions #1653
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
8888a36
392bd39
2055653
5c9df71
10fbb2c
e4ecc7a
0f22d8b
5742b3f
1d0a6ad
2005da0
02f825c
a3bca6a
9614bb4
5cad983
db37cfb
3f2e563
9f99d40
7605d10
13c7db3
d6b4b29
2e45174
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// ReSharper disable UnusedType.Global | ||
// ReSharper disable UnusedMember.Global | ||
// ReSharper disable InconsistentNaming | ||
// ReSharper disable EmptyConstructor | ||
// ReSharper disable MemberCanBeMadeStatic.Local | ||
|
||
using Microsoft.Extensions.Hosting; | ||
//using MQTTnet.AspNetCore; | ||
using MQTTnet.Server; | ||
|
||
namespace MQTTnet.Samples.Server; | ||
|
||
public static class Server_Hosting_Extensions_Samples | ||
{ | ||
|
||
// This could be called as a top-level statement in a Program.cs file | ||
public static Task Start_Single_Line_Server() | ||
=> new HostBuilder().UseMqttServer().Build().RunAsync(); | ||
|
||
public static Task Start_Simple_Server() | ||
{ | ||
var host = new HostBuilder() | ||
.UseMqttServer() | ||
.Build(); | ||
|
||
return host.RunAsync(); | ||
} | ||
|
||
public static Task Start_Server() | ||
{ | ||
var builder = new HostBuilder(); | ||
|
||
builder | ||
.UseMqttServer(mqtt => | ||
{ | ||
mqtt.WithDefaultEndpoint(); | ||
}); | ||
|
||
var host = builder.Build(); | ||
return host.RunAsync(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MQTTnet.Adapter; | ||
using MQTTnet.Diagnostics; | ||
using MQTTnet.Hosting; | ||
using MQTTnet.Implementations; | ||
using MQTTnet.Server; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.Hosting | ||
{ | ||
public static class HostBuilderExtensions | ||
{ | ||
|
||
public static IHostBuilder UseMqttServer(this IHostBuilder hostBuilder) | ||
=> UseMqttServer(hostBuilder, builder => | ||
{ | ||
builder.WithDefaultEndpoint(); | ||
}); | ||
|
||
|
||
public static IHostBuilder UseMqttServer(this IHostBuilder hostBuilder, Action<MqttServerHostingBuilder> configure) | ||
{ | ||
var configureActions = new List<Action<MqttServer>>(); | ||
hostBuilder.ConfigureServices((context, services) => | ||
{ | ||
services.AddSingleton(s => | ||
{ | ||
var builder = new MqttServerHostingBuilder(s, configureActions); | ||
configure(builder); | ||
return builder.Build(); | ||
}); | ||
|
||
var logger = new MqttNetEventLogger(); | ||
|
||
services.AddSingleton<IMqttNetLogger>(logger); | ||
services.AddSingleton<MqttHostedServer>(); | ||
services.AddSingleton<IHostedService>(s => s.GetRequiredService<MqttHostedServer>()); | ||
services.AddSingleton<IHostedService>(s => new MqttServerConfigurationHostedService(s, configureActions)); | ||
services.AddSingleton<MqttServer>(s => s.GetRequiredService<MqttHostedServer>()); | ||
|
||
services.AddSingleton<MqttTcpServerAdapter>(); | ||
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>()); | ||
|
||
}); | ||
return hostBuilder; | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion we should call this project "MQTTnet.Extensions.Hosting" to fit the other extensions (The Asp library was introduced before the naming pattern was established). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YAJeff Ping? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Sorry. I have made those changes internally. Sorry. Been traveling for work. Will check in. There were other changes I've added to make things a little more solid/sound. Will have completed by the end of the weekend. |
||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp3.1'" Include="Microsoft.Extensions.Hosting" Version="3.1.0" /> | ||
<PackageReference Condition="'$(TargetFramework)' == 'net5.0'" Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> | ||
<PackageReference Condition="'$(TargetFramework)' == 'net6.0'" Include="Microsoft.Extensions.Hosting" Version="6.0.0" /> | ||
<PackageReference Condition="'$(TargetFramework)' == 'net7.0'" Include="Microsoft.Extensions.Hosting" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MQTTnet\MQTTnet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using MQTTnet.Server; | ||
using MQTTnet.Adapter; | ||
using MQTTnet.Diagnostics; | ||
|
||
namespace MQTTnet.Hosting | ||
{ | ||
public sealed class MqttHostedServer : MqttServer, IHostedService | ||
{ | ||
public MqttHostedServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger) | ||
: base(options, adapters, logger) | ||
{ | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_ = StartAsync(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return StopAsync(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using MQTTnet.Server; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MQTTnet.Hosting | ||
{ | ||
public class MqttServerConfigurationHostedService : IHostedService | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly List<Action<MqttServer>> _configureActions; | ||
|
||
public MqttServerConfigurationHostedService(IServiceProvider serviceProvider, List<Action<MqttServer>> configureActions) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_configureActions = configureActions; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var server = _serviceProvider.GetRequiredService<MqttServer>(); | ||
_configureActions.ForEach(a => a(server)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MQTTnet.Server; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MQTTnet.Hosting | ||
{ | ||
public class MqttServerHostingBuilder : MqttServerOptionsBuilder | ||
{ | ||
private readonly List<Action<MqttServer>> _configureActions; | ||
|
||
public MqttServerHostingBuilder(IServiceProvider serviceProvider, List<Action<MqttServer>> configureActions) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
_configureActions = configureActions; | ||
} | ||
|
||
public event Func<ApplicationMessageNotConsumedEventArgs, Task> ApplicationMessageNotConsumedAsync | ||
{ | ||
add => _configureActions.Add(server => server.ApplicationMessageNotConsumedAsync += value); | ||
remove => _configureActions.Add(server => server.ApplicationMessageNotConsumedAsync -= value); | ||
} | ||
|
||
public event Func<ClientAcknowledgedPublishPacketEventArgs, Task> ClientAcknowledgedPublishPacketAsync | ||
{ | ||
add => _configureActions.Add(server => server.ClientAcknowledgedPublishPacketAsync += value); | ||
remove => _configureActions.Add(server => server.ClientAcknowledgedPublishPacketAsync -= value); | ||
} | ||
|
||
public event Func<ClientConnectedEventArgs, Task> ClientConnectedAsync | ||
{ | ||
add => _configureActions.Add(server => server.ClientConnectedAsync += value); | ||
remove => _configureActions.Add(server => server.ClientConnectedAsync -= value); | ||
} | ||
|
||
public event Func<ClientDisconnectedEventArgs, Task> ClientDisconnectedAsync | ||
{ | ||
add => _configureActions.Add(server => server.ClientDisconnectedAsync += value); | ||
remove => _configureActions.Add(server => server.ClientDisconnectedAsync -= value); | ||
} | ||
|
||
public event Func<ClientSubscribedTopicEventArgs, Task> ClientSubscribedTopicAsync | ||
{ | ||
add => _configureActions.Add(server => server.ClientSubscribedTopicAsync += value); | ||
remove => _configureActions.Add(server => server.ClientSubscribedTopicAsync -= value); | ||
} | ||
|
||
public event Func<ClientUnsubscribedTopicEventArgs, Task> ClientUnsubscribedTopicAsync | ||
{ | ||
add => _configureActions.Add(server => server.ClientUnsubscribedTopicAsync += value); | ||
remove => _configureActions.Add(server => server.ClientUnsubscribedTopicAsync -= value); | ||
} | ||
|
||
public event Func<InterceptingPacketEventArgs, Task> InterceptingInboundPacketAsync | ||
{ | ||
add => _configureActions.Add(server => server.InterceptingInboundPacketAsync += value); | ||
remove => _configureActions.Add(server => server.InterceptingInboundPacketAsync -= value); | ||
} | ||
|
||
public event Func<InterceptingPacketEventArgs, Task> InterceptingOutboundPacketAsync | ||
{ | ||
add => _configureActions.Add(server => server.InterceptingOutboundPacketAsync += value); | ||
remove => _configureActions.Add(server => server.InterceptingOutboundPacketAsync -= value); | ||
} | ||
|
||
public event Func<InterceptingPublishEventArgs, Task> InterceptingPublishAsync | ||
{ | ||
add => _configureActions.Add(server => server.InterceptingPublishAsync += value); | ||
remove => _configureActions.Add(server => server.InterceptingPublishAsync -= value); | ||
} | ||
|
||
public event Func<InterceptingSubscriptionEventArgs, Task> InterceptingSubscriptionAsync | ||
{ | ||
add => _configureActions.Add(server => server.InterceptingSubscriptionAsync += value); | ||
remove => _configureActions.Add(server => server.InterceptingSubscriptionAsync -= value); | ||
} | ||
|
||
public event Func<InterceptingUnsubscriptionEventArgs, Task> InterceptingUnsubscriptionAsync | ||
{ | ||
add => _configureActions.Add(server => server.InterceptingUnsubscriptionAsync += value); | ||
remove => _configureActions.Add(server => server.InterceptingUnsubscriptionAsync -= value); | ||
} | ||
|
||
public event Func<LoadingRetainedMessagesEventArgs, Task> LoadingRetainedMessageAsync | ||
{ | ||
add => _configureActions.Add(server => server.LoadingRetainedMessageAsync += value); | ||
remove => _configureActions.Add(server => server.LoadingRetainedMessageAsync -= value); | ||
} | ||
|
||
public event Func<EventArgs, Task> PreparingSessionAsync | ||
{ | ||
add => _configureActions.Add(server => server.PreparingSessionAsync += value); | ||
remove => _configureActions.Add(server => server.PreparingSessionAsync -= value); | ||
} | ||
|
||
public event Func<RetainedMessageChangedEventArgs, Task> RetainedMessageChangedAsync | ||
{ | ||
add => _configureActions.Add(server => server.RetainedMessageChangedAsync += value); | ||
remove => _configureActions.Add(server => server.RetainedMessageChangedAsync -= value); | ||
} | ||
|
||
public event Func<EventArgs, Task> RetainedMessagesClearedAsync | ||
{ | ||
add => _configureActions.Add(server => server.RetainedMessagesClearedAsync += value); | ||
remove => _configureActions.Add(server => server.RetainedMessagesClearedAsync -= value); | ||
} | ||
|
||
public event Func<SessionDeletedEventArgs, Task> SessionDeletedAsync | ||
{ | ||
add => _configureActions.Add(server => server.SessionDeletedAsync += value); | ||
remove => _configureActions.Add(server => server.SessionDeletedAsync -= value); | ||
} | ||
|
||
public event Func<EventArgs, Task> StartedAsync | ||
{ | ||
add => _configureActions.Add(server => server.StartedAsync += value); | ||
remove => _configureActions.Add(server => server.StartedAsync -= value); | ||
} | ||
|
||
public event Func<EventArgs, Task> StoppedAsync | ||
{ | ||
add => _configureActions.Add(server => server.StoppedAsync += value); | ||
remove => _configureActions.Add(server => server.StoppedAsync -= value); | ||
} | ||
|
||
public event Func<ValidatingConnectionEventArgs, Task> ValidatingConnectionAsync | ||
{ | ||
add => _configureActions.Add(server => server.ValidatingConnectionAsync += value); | ||
remove => _configureActions.Add(server => server.ValidatingConnectionAsync -= value); | ||
} | ||
|
||
public IServiceProvider ServiceProvider { get; } | ||
|
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.