@@ -37,11 +37,7 @@ public static IServiceCollection AddRabbitMqClient(this IServiceCollection servi
3737 /// <returns>Service collection.</returns>
3838 public static IServiceCollection AddExchange ( this IServiceCollection services , string exchangeName , IConfiguration configuration )
3939 {
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 ) ;
4541
4642 var options = new RabbitMqExchangeOptions ( ) ;
4743 configuration . Bind ( options ) ;
@@ -57,11 +53,7 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
5753 /// <returns>Service collection.</returns>
5854 public static IServiceCollection AddExchange ( this IServiceCollection services , string exchangeName , RabbitMqExchangeOptions options )
5955 {
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 ) ;
6557
6658 var exchangeOptions = options ?? new RabbitMqExchangeOptions ( ) ;
6759 var exchange = new RabbitMqExchange { Name = exchangeName , Options = exchangeOptions } ;
@@ -73,6 +65,15 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
7365 return services ;
7466 }
7567
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+
7677 /// <summary>
7778 /// Add transient message handler.
7879 /// </summary>
@@ -83,10 +84,7 @@ public static IServiceCollection AddExchange(this IServiceCollection services, s
8384 public static IServiceCollection AddMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
8485 where T : class , IMessageHandler
8586 {
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 ( ) ) ;
9088 }
9189
9290 /// <summary>
@@ -99,10 +97,7 @@ public static IServiceCollection AddMessageHandlerTransient<T>(this IServiceColl
9997 public static IServiceCollection AddMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
10098 where T : class , IMessageHandler
10199 {
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 ( ) ) ;
106101 }
107102
108103 /// <summary>
@@ -115,10 +110,7 @@ public static IServiceCollection AddMessageHandlerTransient<T>(this IServiceColl
115110 public static IServiceCollection AddMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
116111 where T : class , IMessageHandler
117112 {
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 ( ) ) ;
122114 }
123115
124116 /// <summary>
@@ -131,10 +123,7 @@ public static IServiceCollection AddMessageHandlerSingleton<T>(this IServiceColl
131123 public static IServiceCollection AddMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
132124 where T : class , IMessageHandler
133125 {
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 ( ) ) ;
138127 }
139128
140129 /// <summary>
@@ -147,10 +136,7 @@ public static IServiceCollection AddMessageHandlerSingleton<T>(this IServiceColl
147136 public static IServiceCollection AddAsyncMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
148137 where T : class , IAsyncMessageHandler
149138 {
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 ( ) ) ;
154140 }
155141
156142 /// <summary>
@@ -163,10 +149,7 @@ public static IServiceCollection AddAsyncMessageHandlerTransient<T>(this IServic
163149 public static IServiceCollection AddAsyncMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
164150 where T : class , IAsyncMessageHandler
165151 {
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 ( ) ) ;
170153 }
171154
172155 /// <summary>
@@ -179,10 +162,7 @@ public static IServiceCollection AddAsyncMessageHandlerTransient<T>(this IServic
179162 public static IServiceCollection AddAsyncMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
180163 where T : class , IAsyncMessageHandler
181164 {
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 ( ) ) ;
186166 }
187167
188168 /// <summary>
@@ -195,10 +175,7 @@ public static IServiceCollection AddAsyncMessageHandlerSingleton<T>(this IServic
195175 public static IServiceCollection AddAsyncMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
196176 where T : class , IAsyncMessageHandler
197177 {
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 ( ) ) ;
202179 }
203180
204181 /// <summary>
@@ -211,10 +188,7 @@ public static IServiceCollection AddAsyncMessageHandlerSingleton<T>(this IServic
211188 public static IServiceCollection AddNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
212189 where T : class , INonCyclicMessageHandler
213190 {
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 ( ) ) ;
218192 }
219193
220194 /// <summary>
@@ -227,10 +201,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerTransient<T>(this ISe
227201 public static IServiceCollection AddNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
228202 where T : class , INonCyclicMessageHandler
229203 {
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 ( ) ) ;
234205 }
235206
236207 /// <summary>
@@ -243,10 +214,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerTransient<T>(this ISe
243214 public static IServiceCollection AddNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
244215 where T : class , INonCyclicMessageHandler
245216 {
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 ( ) ) ;
250218 }
251219
252220 /// <summary>
@@ -259,10 +227,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerSingleton<T>(this ISe
259227 public static IServiceCollection AddNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
260228 where T : class , INonCyclicMessageHandler
261229 {
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 ( ) ) ;
266231 }
267232
268233 /// <summary>
@@ -275,10 +240,7 @@ public static IServiceCollection AddNonCyclicMessageHandlerSingleton<T>(this ISe
275240 public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , string routingKey )
276241 where T : class , IAsyncNonCyclicMessageHandler
277242 {
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 ( ) ) ;
282244 }
283245
284246 /// <summary>
@@ -291,10 +253,7 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient<T>(thi
291253 public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
292254 where T : class , IAsyncNonCyclicMessageHandler
293255 {
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 ( ) ) ;
298257 }
299258
300259 /// <summary>
@@ -307,10 +266,7 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerTransient<T>(thi
307266 public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , string routingKey )
308267 where T : class , IAsyncNonCyclicMessageHandler
309268 {
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 ( ) ) ;
314270 }
315271
316272 /// <summary>
@@ -323,7 +279,24 @@ public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton<T>(thi
323279 public static IServiceCollection AddAsyncNonCyclicMessageHandlerSingleton < T > ( this IServiceCollection services , IEnumerable < string > routingKeys )
324280 where T : class , IAsyncNonCyclicMessageHandler
325281 {
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 > ( ) ;
327300 var router = new MessageHandlerRouter { Type = typeof ( T ) , RoutingKeys = routingKeys . ToList ( ) } ;
328301 services . Add ( new ServiceDescriptor ( typeof ( MessageHandlerRouter ) , router ) ) ;
329302 return services ;
0 commit comments