44
55using System ;
66using Microsoft . Extensions . DependencyInjection ;
7- using Microsoft . Extensions . Hosting ;
7+ using Microsoft . Extensions . DependencyInjection . Extensions ;
88using MQTTnet . Adapter ;
99using MQTTnet . Diagnostics ;
1010using MQTTnet . Implementations ;
@@ -14,76 +14,102 @@ namespace MQTTnet.AspNetCore
1414{
1515public static class ServiceCollectionExtensions
1616{
17- public static IServiceCollection AddMqttServer ( this IServiceCollection serviceCollection , Action < MqttServerOptionsBuilder > configure = null )
17+ public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , MqttServerOptions options )
1818{
19- if ( serviceCollection is null )
19+ if ( services == null )
2020{
21- throw new ArgumentNullException ( nameof ( serviceCollection ) ) ;
21+ throw new ArgumentNullException ( nameof ( services ) ) ;
2222}
2323
24- serviceCollection . AddMqttConnectionHandler ( ) ;
25- serviceCollection . AddHostedMqttServer ( configure ) ;
26-
27- return serviceCollection ;
28- }
29-
30- public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , MqttServerOptions options )
31- {
32- if ( options == null ) throw new ArgumentNullException ( nameof ( options ) ) ;
24+ if ( options == null )
25+ {
26+ throw new ArgumentNullException ( nameof ( options ) ) ;
27+ }
3328
3429services . AddSingleton ( options ) ;
35-
3630services . AddHostedMqttServer ( ) ;
3731
3832return services ;
3933}
4034
41- public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , Action < MqttServerOptionsBuilder > configure = null )
35+ public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , Action < MqttServerOptionsBuilder > configure )
4236{
43- services . AddSingleton ( s =>
37+ if ( services == null )
4438{
45- var serverOptionsBuilder = new MqttServerOptionsBuilder ( ) ;
46- configure ? . Invoke ( serverOptionsBuilder ) ;
47- return serverOptionsBuilder . Build ( ) ;
48- } ) ;
39+ throw new ArgumentNullException ( nameof ( services ) ) ;
40+ }
4941
50- services . AddHostedMqttServer ( ) ;
42+ if ( configure == null )
43+ {
44+ throw new ArgumentNullException ( nameof ( configure ) ) ;
45+ }
5146
52- return services ;
47+ var serverOptionsBuilder = new MqttServerOptionsBuilder ( ) ;
48+ configure . Invoke ( serverOptionsBuilder ) ;
49+ var options = serverOptionsBuilder . Build ( ) ;
50+
51+ return AddHostedMqttServer ( services , options ) ;
52+ }
53+
54+ public static void AddHostedMqttServer ( this IServiceCollection services )
55+ {
56+ // The user may have these services already registered.
57+ services . TryAddSingleton < IMqttNetLogger > ( MqttNetNullLogger . Instance ) ;
58+ services . TryAddSingleton ( new MqttFactory ( ) ) ;
59+
60+ services . AddSingleton < MqttHostedServer > ( ) ;
61+ services . AddHostedService < MqttHostedServer > ( ) ;
5362}
5463
5564public static IServiceCollection AddHostedMqttServerWithServices ( this IServiceCollection services , Action < AspNetMqttServerOptionsBuilder > configure )
5665{
57- services . AddSingleton ( s =>
66+ if ( services == null )
5867{
59- var builder = new AspNetMqttServerOptionsBuilder ( s ) ;
60- configure ( builder ) ;
61- return builder . Build ( ) ;
62- } ) ;
68+ throw new ArgumentNullException ( nameof ( services ) ) ;
69+ }
70+
71+ services . AddSingleton (
72+ s=>
73+ {
74+ var builder = new AspNetMqttServerOptionsBuilder ( s ) ;
75+ configure ( builder ) ;
76+ return builder . Build ( ) ;
77+ } ) ;
6378
6479services . AddHostedMqttServer ( ) ;
6580
6681return services ;
6782}
6883
69- static IServiceCollection AddHostedMqttServer ( this IServiceCollection services )
84+ public static IServiceCollection AddMqttConnectionHandler ( this IServiceCollection services )
7085{
71- var logger = new MqttNetEventLogger ( ) ;
72-
73- services . AddSingleton < IMqttNetLogger > ( logger ) ;
74- services . AddSingleton < MqttHostedServer > ( ) ;
75- services . AddSingleton < IHostedService > ( s=> s . GetService < MqttHostedServer > ( ) ) ;
76- services . AddSingleton < MqttServer > ( s=> s . GetService < MqttHostedServer > ( ) ) ;
86+ services . AddSingleton < MqttConnectionHandler > ( ) ;
87+ services . AddSingleton < IMqttServerAdapter > ( s=> s . GetService < MqttConnectionHandler > ( ) ) ;
7788
7889return services ;
7990}
8091
81- public static IServiceCollection AddMqttWebSocketServerAdapter ( this IServiceCollection services )
92+ public static void AddMqttLogger ( this IServiceCollection services , IMqttNetLogger logger )
8293{
83- services . AddSingleton < MqttWebSocketServerAdapter > ( ) ;
84- services . AddSingleton < IMqttServerAdapter > ( s=> s . GetService < MqttWebSocketServerAdapter > ( ) ) ;
94+ if ( services == null )
95+ {
96+ throw new ArgumentNullException ( nameof ( services ) ) ;
97+ }
8598
86- return services ;
99+ services . AddSingleton ( logger ) ;
100+ }
101+
102+ public static IServiceCollection AddMqttServer ( this IServiceCollection serviceCollection , Action < MqttServerOptionsBuilder > configure = null )
103+ {
104+ if ( serviceCollection is null )
105+ {
106+ throw new ArgumentNullException ( nameof ( serviceCollection ) ) ;
107+ }
108+
109+ serviceCollection . AddMqttConnectionHandler ( ) ;
110+ serviceCollection . AddHostedMqttServer ( configure ) ;
111+
112+ return serviceCollection ;
87113}
88114
89115public static IServiceCollection AddMqttTcpServerAdapter ( this IServiceCollection services )
@@ -94,12 +120,12 @@ public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection
94120return services ;
95121}
96122
97- public static IServiceCollection AddMqttConnectionHandler ( this IServiceCollection services )
123+ public static IServiceCollection AddMqttWebSocketServerAdapter ( this IServiceCollection services )
98124{
99- services . AddSingleton < MqttConnectionHandler > ( ) ;
100- services . AddSingleton < IMqttServerAdapter > ( s=> s . GetService < MqttConnectionHandler > ( ) ) ;
125+ services . AddSingleton < MqttWebSocketServerAdapter > ( ) ;
126+ services . AddSingleton < IMqttServerAdapter > ( s=> s . GetService < MqttWebSocketServerAdapter > ( ) ) ;
101127
102128return services ;
103129}
104130}
105- }
131+ }