@@ -37,11 +37,7 @@ public static IServiceCollection AddRabbitMqClient(this IServiceCollection servi
37
37
/// <returns>Service collection.</returns>
38
38
public static IServiceCollection AddExchange ( this IServiceCollection services , string exchangeName , IConfiguration configuration )
39
39
{
40
- var exchangeExists = services . Any ( x => x . ServiceType == typeof ( RabbitMqExchange )
41
- && x . Lifetime == ServiceLifetime . Singleton
42
- && string . Equals ( ( ( ExchangeServiceDescriptor ) x ) . ExchangeName , exchangeName , StringComparison . OrdinalIgnoreCase ) ) ;
43
- if ( exchangeExists )
44
- throw new ArgumentException ( $ "Exchange { exchangeName } has already been added!") ;
40
+ CheckExchangeExists ( services , exchangeName ) ;
45
41
46
42
var options = new RabbitMqExchangeOptions ( ) ;
47
43
configuration . Bind ( options ) ;
@@ -57,11 +53,7 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
57
53
/// <returns>Service collection.</returns>
58
54
public static IServiceCollection AddExchange ( this IServiceCollection services , string exchangeName , RabbitMqExchangeOptions options )
59
55
{
60
- var exchangeExists = services . Any ( x => x . ServiceType == typeof ( RabbitMqExchange )
61
- && x . Lifetime == ServiceLifetime . Singleton
62
- && string . Equals ( ( ( ExchangeServiceDescriptor ) x ) . ExchangeName , exchangeName , StringComparison . OrdinalIgnoreCase ) ) ;
63
- if ( exchangeExists )
64
- throw new ArgumentException ( $ "Exchange { exchangeName } has already been added!") ;
56
+ CheckExchangeExists ( services , exchangeName ) ;
65
57
66
58
var exchangeOptions = options ?? new RabbitMqExchangeOptions ( ) ;
67
59
var exchange = new RabbitMqExchange { Name = exchangeName , Options = exchangeOptions } ;
@@ -73,6 +65,15 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
73
65
return services ;
74
66
}
75
67
68
+ static void CheckExchangeExists ( IServiceCollection services , string exchangeName )
69
+ {
70
+ var exchangeExists = services . Any ( x => x . ServiceType == typeof ( RabbitMqExchange )
71
+ && x . Lifetime == ServiceLifetime . Singleton
72
+ && string . Equals ( ( ( ExchangeServiceDescriptor ) x ) . ExchangeName , exchangeName , StringComparison . OrdinalIgnoreCase ) ) ;
73
+ if ( exchangeExists )
74
+ throw new ArgumentException ( $ "Exchange { exchangeName } has already been added!") ;
75
+ }
76
+
76
77
/// <summary>
77
78
/// Add transient message handler.
78
79
/// </summary>
@@ -83,10 +84,7 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
83
84
public static IServiceCollection AddMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
84
85
where T : class , IMessageHandler
85
86
{
86
- services . AddTransient < IMessageHandler , T > ( ) ;
87
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
88
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
89
- return services ;
87
+ return services . AddInstanceTransient < IMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
90
88
}
91
89
92
90
/// <summary>
@@ -99,10 +97,7 @@ public static IServiceCollection AddMessageHandlerTransient<T>(this IServiceColl
99
97
public static IServiceCollection AddMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
100
98
where T : class , IMessageHandler
101
99
{
102
- services . AddTransient < IMessageHandler , T > ( ) ;
103
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
104
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
105
- return services ;
100
+ return services . AddInstanceTransient < IMessageHandler , T > ( routingKeys . ToList ( ) ) ;
106
101
}
107
102
108
103
/// <summary>
@@ -115,10 +110,7 @@ public static IServiceCollection AddMessageHandlerTransient<T>(this IServiceColl
115
110
public static IServiceCollection AddMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
116
111
where T : class , IMessageHandler
117
112
{
118
- services . AddSingleton < IMessageHandler , T > ( ) ;
119
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
120
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
121
- return services ;
113
+ return services . AddInstanceSingleton < IMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
122
114
}
123
115
124
116
/// <summary>
@@ -131,10 +123,7 @@ public static IServiceCollection AddMessageHandlerSingleton<T>(this IServiceColl
131
123
public static IServiceCollection AddMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
132
124
where T : class , IMessageHandler
133
125
{
134
- services . AddSingleton < IMessageHandler , T > ( ) ;
135
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
136
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
137
- return services ;
126
+ return services . AddInstanceSingleton < IMessageHandler , T > ( routingKeys . ToList ( ) ) ;
138
127
}
139
128
140
129
/// <summary>
@@ -147,10 +136,7 @@ public static IServiceCollection AddMessageHandlerSingleton<T>(this IServiceColl
147
136
public static IServiceCollection AddAsyncMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
148
137
where T : class , IAsyncMessageHandler
149
138
{
150
- services . AddTransient < IAsyncMessageHandler , T > ( ) ;
151
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
152
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
153
- return services ;
139
+ return services . AddInstanceTransient < IAsyncMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
154
140
}
155
141
156
142
/// <summary>
@@ -163,10 +149,7 @@ public static IServiceCollection AddAsyncMessageHandlerTransient<T>(this IServic
163
149
public static IServiceCollection AddAsyncMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
164
150
where T : class , IAsyncMessageHandler
165
151
{
166
- services . AddTransient < IAsyncMessageHandler , T > ( ) ;
167
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
168
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
169
- return services ;
152
+ return services . AddInstanceTransient < IAsyncMessageHandler , T > ( routingKeys . ToList ( ) ) ;
170
153
}
171
154
172
155
/// <summary>
@@ -179,10 +162,7 @@ public static IServiceCollection AddAsyncMessageHandlerTransient<T>(this IServic
179
162
public static IServiceCollection AddAsyncMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
180
163
where T : class , IAsyncMessageHandler
181
164
{
182
- services . AddSingleton < IAsyncMessageHandler , T > ( ) ;
183
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
184
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
185
- return services ;
165
+ return services . AddInstanceSingleton < IAsyncMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
186
166
}
187
167
188
168
/// <summary>
@@ -195,10 +175,7 @@ public static IServiceCollection AddAsyncMessageHandlerSingleton<T>(this IServic
195
175
public static IServiceCollection AddAsyncMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
196
176
where T : class , IAsyncMessageHandler
197
177
{
198
- services . AddSingleton < IAsyncMessageHandler , T > ( ) ;
199
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
200
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
201
- return services ;
178
+ return services . AddInstanceSingleton < IAsyncMessageHandler , T > ( routingKeys . ToList ( ) ) ;
202
179
}
203
180
204
181
/// <summary>
@@ -211,10 +188,7 @@ public static IServiceCollection AddAsyncMessageHandlerSingleton<T>(this IServic
211
188
public static IServiceCollection AddNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
212
189
where T : class , INonCyclicMessageHandler
213
190
{
214
- services . AddTransient < INonCyclicMessageHandler , T > ( ) ;
215
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
216
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
217
- return services ;
191
+ return services . AddInstanceTransient < INonCyclicMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
218
192
}
219
193
220
194
/// <summary>
@@ -227,10 +201,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerTransient<T>(this ISe
227
201
public static IServiceCollection AddNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
228
202
where T : class , INonCyclicMessageHandler
229
203
{
230
- services . AddTransient < INonCyclicMessageHandler , T > ( ) ;
231
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
232
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
233
- return services ;
204
+ return services . AddInstanceTransient < INonCyclicMessageHandler , T > ( routingKeys . ToList ( ) ) ;
234
205
}
235
206
236
207
/// <summary>
@@ -243,10 +214,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerTransient<T>(this ISe
243
214
public static IServiceCollection AddNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
244
215
where T : class , INonCyclicMessageHandler
245
216
{
246
- services . AddSingleton < INonCyclicMessageHandler , T > ( ) ;
247
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
248
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
249
- return services ;
217
+ return services . AddInstanceSingleton < INonCyclicMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
250
218
}
251
219
252
220
/// <summary>
@@ -259,10 +227,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerSingleton<T>(this ISe
259
227
public static IServiceCollection AddNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
260
228
where T : class , INonCyclicMessageHandler
261
229
{
262
- services . AddSingleton < INonCyclicMessageHandler , T > ( ) ;
263
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
264
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
265
- return services ;
230
+ return services . AddInstanceSingleton < INonCyclicMessageHandler , T > ( routingKeys . ToList ( ) ) ;
266
231
}
267
232
268
233
/// <summary>
@@ -275,10 +240,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerSingleton<T>(this ISe
275
240
public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
276
241
where T : class , IAsyncNonCyclicMessageHandler
277
242
{
278
- services . AddTransient < IAsyncNonCyclicMessageHandler , T > ( ) ;
279
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
280
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
281
- return services ;
243
+ return services . AddInstanceTransient < IAsyncNonCyclicMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
282
244
}
283
245
284
246
/// <summary>
@@ -291,10 +253,7 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient<T>(thi
291
253
public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
292
254
where T : class , IAsyncNonCyclicMessageHandler
293
255
{
294
- services . AddTransient < IAsyncNonCyclicMessageHandler , T > ( ) ;
295
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
296
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
297
- return services ;
256
+ return services . AddInstanceTransient < IAsyncNonCyclicMessageHandler , T > ( routingKeys . ToList ( ) ) ;
298
257
}
299
258
300
259
/// <summary>
@@ -307,10 +266,7 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient<T>(thi
307
266
public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
308
267
where T : class , IAsyncNonCyclicMessageHandler
309
268
{
310
- services . AddSingleton < IAsyncNonCyclicMessageHandler , T > ( ) ;
311
- var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = new [ ] { routingKey } . ToList ( ) } ;
312
- services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
313
- return services ;
269
+ return services . AddInstanceSingleton < IAsyncNonCyclicMessageHandler , T > ( new [ ] { routingKey } . ToList ( ) ) ;
314
270
}
315
271
316
272
/// <summary>
@@ -323,7 +279,24 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton<T>(thi
323
279
public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
324
280
where T : class , IAsyncNonCyclicMessageHandler
325
281
{
326
- services . AddSingleton < IAsyncNonCyclicMessageHandler , T > ( ) ;
282
+ return services . AddInstanceSingleton < IAsyncNonCyclicMessageHandler , T > ( routingKeys . ToList ( ) ) ;
283
+ }
284
+
285
+ static IServiceCollection AddInstanceTransient < U , T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
286
+ where U : class
287
+ where T : class , U
288
+ {
289
+ services . AddTransient < U , T > ( ) ;
290
+ var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
291
+ services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
292
+ return services ;
293
+ }
294
+
295
+ static IServiceCollection AddInstanceSingleton < U , T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
296
+ where U : class
297
+ where T : class , U
298
+ {
299
+ services . AddSingleton < U , T > ( ) ;
327
300
var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
328
301
services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
329
302
return services ;
0 commit comments