4
4
5
5
using System ;
6
6
using Microsoft . Extensions . DependencyInjection ;
7
- using Microsoft . Extensions . Hosting ;
7
+ using Microsoft . Extensions . DependencyInjection . Extensions ;
8
8
using MQTTnet . Adapter ;
9
9
using MQTTnet . Diagnostics ;
10
10
using MQTTnet . Implementations ;
@@ -14,76 +14,102 @@ namespace MQTTnet.AspNetCore
14
14
{
15
15
public static class ServiceCollectionExtensions
16
16
{
17
- public static IServiceCollection AddMqttServer ( this IServiceCollection serviceCollection , Action < MqttServerOptionsBuilder > configure = null )
17
+ public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , MqttServerOptions options )
18
18
{
19
- if ( serviceCollection is null )
19
+ if ( services == null )
20
20
{
21
- throw new ArgumentNullException ( nameof ( serviceCollection ) ) ;
21
+ throw new ArgumentNullException ( nameof ( services ) ) ;
22
22
}
23
23
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
+ }
33
28
34
29
services . AddSingleton ( options ) ;
35
-
36
30
services . AddHostedMqttServer ( ) ;
37
31
38
32
return services ;
39
33
}
40
34
41
- public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , Action < MqttServerOptionsBuilder > configure = null )
35
+ public static IServiceCollection AddHostedMqttServer ( this IServiceCollection services , Action < MqttServerOptionsBuilder > configure )
42
36
{
43
- services . AddSingleton ( s =>
37
+ if ( services == null )
44
38
{
45
- var serverOptionsBuilder = new MqttServerOptionsBuilder ( ) ;
46
- configure ? . Invoke ( serverOptionsBuilder ) ;
47
- return serverOptionsBuilder . Build ( ) ;
48
- } ) ;
39
+ throw new ArgumentNullException ( nameof ( services ) ) ;
40
+ }
49
41
50
- services . AddHostedMqttServer ( ) ;
42
+ if ( configure == null )
43
+ {
44
+ throw new ArgumentNullException ( nameof ( configure ) ) ;
45
+ }
51
46
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 > ( ) ;
53
62
}
54
63
55
64
public static IServiceCollection AddHostedMqttServerWithServices ( this IServiceCollection services , Action < AspNetMqttServerOptionsBuilder > configure )
56
65
{
57
- services . AddSingleton ( s =>
66
+ if ( services == null )
58
67
{
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
+ } ) ;
63
78
64
79
services . AddHostedMqttServer ( ) ;
65
80
66
81
return services ;
67
82
}
68
83
69
- static IServiceCollection AddHostedMqttServer ( this IServiceCollection services )
84
+ public static IServiceCollection AddMqttConnectionHandler ( this IServiceCollection services )
70
85
{
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 > ( ) ) ;
77
88
78
89
return services ;
79
90
}
80
91
81
- public static IServiceCollection AddMqttWebSocketServerAdapter ( this IServiceCollection services )
92
+ public static void AddMqttLogger ( this IServiceCollection services , IMqttNetLogger logger )
82
93
{
83
- services . AddSingleton < MqttWebSocketServerAdapter > ( ) ;
84
- services . AddSingleton < IMqttServerAdapter > ( s => s . GetService < MqttWebSocketServerAdapter > ( ) ) ;
94
+ if ( services == null )
95
+ {
96
+ throw new ArgumentNullException ( nameof ( services ) ) ;
97
+ }
85
98
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 ;
87
113
}
88
114
89
115
public static IServiceCollection AddMqttTcpServerAdapter ( this IServiceCollection services )
@@ -94,12 +120,12 @@ public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection
94
120
return services ;
95
121
}
96
122
97
- public static IServiceCollection AddMqttConnectionHandler ( this IServiceCollection services )
123
+ public static IServiceCollection AddMqttWebSocketServerAdapter ( this IServiceCollection services )
98
124
{
99
- services . AddSingleton < MqttConnectionHandler > ( ) ;
100
- services . AddSingleton < IMqttServerAdapter > ( s => s . GetService < MqttConnectionHandler > ( ) ) ;
125
+ services . AddSingleton < MqttWebSocketServerAdapter > ( ) ;
126
+ services . AddSingleton < IMqttServerAdapter > ( s => s . GetService < MqttWebSocketServerAdapter > ( ) ) ;
101
127
102
128
return services ;
103
129
}
104
130
}
105
- }
131
+ }
0 commit comments