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

Support for hosting extensions #1653

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions MQTTnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp", "Source\M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.AspTestApp", "Source\MQTTnet.AspTestApp\MQTTnet.AspTestApp.csproj", "{72867E4C-4E15-4E8E-8FAB-AE9253286BBC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Hosting", "Source\MQTTnet.Hosting\MQTTnet.Hosting.csproj", "{B53FC20B-862C-4F8F-B9FF-E8C8D76A870D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{72867E4C-4E15-4E8E-8FAB-AE9253286BBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72867E4C-4E15-4E8E-8FAB-AE9253286BBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72867E4C-4E15-4E8E-8FAB-AE9253286BBC}.Release|Any CPU.Build.0 = Release|Any CPU
{B53FC20B-862C-4F8F-B9FF-E8C8D76A870D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B53FC20B-862C-4F8F-B9FF-E8C8D76A870D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B53FC20B-862C-4F8F-B9FF-E8C8D76A870D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B53FC20B-862C-4F8F-B9FF-E8C8D76A870D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions Samples/MQTTnet.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ProjectReference Include="..\Source\MQTTnet.Extensions.ManagedClient\MQTTnet.Extensions.ManagedClient.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Extensions.WebSocket4Net\MQTTnet.Extensions.WebSocket4Net.csproj" />
<ProjectReference Include="..\Source\MQTTnet.Hosting\MQTTnet.Hosting.csproj" />
chkr1011 marked this conversation as resolved.
Show resolved Hide resolved
<ProjectReference Include="..\Source\MQTTnet\MQTTnet.csproj" />
</ItemGroup>

Expand Down
46 changes: 46 additions & 0 deletions Samples/Server/Server_Hosting_Extensions_Samples.cs
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();
}
}
50 changes: 50 additions & 0 deletions Source/MQTTnet.Hosting/HostBuilderExtensions.cs
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;
}

}
}
19 changes: 19 additions & 0 deletions Source/MQTTnet.Hosting/MQTTnet.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
Copy link
Collaborator

Choose a reason for hiding this comment

The 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).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YAJeff Ping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
31 changes: 31 additions & 0 deletions Source/MQTTnet.Hosting/MqttHostedServer.cs
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();
}
}
}
37 changes: 37 additions & 0 deletions Source/MQTTnet.Hosting/MqttServerConfigurationHostedService.cs
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;
}
}
}
138 changes: 138 additions & 0 deletions Source/MQTTnet.Hosting/MqttServerHostingBuilder.cs
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; }

}
}
25 changes: 25 additions & 0 deletions Source/MQTTnet.Tests/MQTTnet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@
<ProjectReference Include="..\..\Source\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj" />
<ProjectReference Include="..\..\Source\MQTTnet\MQTTnet.csproj" />
<ProjectReference Include="..\MQTTnet.Extensions.WebSocket4Net\MQTTnet.Extensions.WebSocket4Net.csproj" />
<ProjectReference Condition="'$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0'" Include="..\MQTTnet.Hosting\MQTTnet.Hosting.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>3.1.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>5.0.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>6.0.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Hosting">
<Version>7.0.0</Version>
</PackageReference>
</ItemGroup>

</Project>
Loading