1- /// <summary>
1+ using Microsoft . AspNetCore . Builder ;
2+ using Microsoft . Extensions . DependencyInjection ;
3+ using System ;
4+ /// <summary>
25///
36/// </summary>
47public static class ServiceCollectionExtensions
@@ -15,7 +18,7 @@ public static class ServiceCollectionExtensions
1518 /// <param name="eventBusOptionAction"></param>
1619 /// <param name="moduleOptions"></param>
1720 /// <returns></returns>
18- public static IServiceProvider AddRabbitMQEventBus ( this IServiceCollection services , string endpoint , int port , string username , string password , string visualHost , Action < RabbitMQEventBusConnectionConfigurationBuild > eventBusOptionAction , Action < RabbitMQEventBusModuleOption > moduleOptions = null )
21+ public static IServiceCollection AddRabbitMQEventBus ( this IServiceCollection services , string endpoint , int port , string username , string password , string visualHost , Action < RabbitMQEventBusConnectionConfigurationBuild > eventBusOptionAction , Action < RabbitMQEventBusModuleOption > moduleOptions = null )
1922 => AddRabbitMQEventBus ( services , ( ) => $ "amqp://{ username } :{ password } @{ endpoint } :{ port } /{ visualHost } ", eventBusOptionAction , moduleOptions ) ;
2023
2124 /// <summary>
@@ -25,7 +28,7 @@ public static IServiceProvider AddRabbitMQEventBus(this IServiceCollection servi
2528 /// <param name="connectionAction">使用匿名函数取得连接字符串,用来兼容使用Consul获取服务地址的情况</param>
2629 /// <param name="eventBusOptionAction"></param>
2730 /// <returns></returns>
28- public static IServiceProvider AddRabbitMQEventBus ( this IServiceCollection services , Func < string > connectionAction , Action < RabbitMQEventBusConnectionConfigurationBuild > eventBusOptionAction , Action < RabbitMQEventBusModuleOption > moduleOptions = null )
31+ public static IServiceCollection AddRabbitMQEventBus ( this IServiceCollection services , Func < string > connectionAction , Action < RabbitMQEventBusConnectionConfigurationBuild > eventBusOptionAction , Action < RabbitMQEventBusModuleOption > moduleOptions = null )
2932 {
3033 RabbitMQEventBusConnectionConfiguration configuration = new ( ) ;
3134 RabbitMQEventBusConnectionConfigurationBuild configurationBuild = new ( configuration ) ;
@@ -38,42 +41,42 @@ public static IServiceProvider AddRabbitMQEventBus(this IServiceCollection servi
3841 logger . LogInformation ( "RabbitMQ event bus connected." ) ;
3942 return connection ;
4043 } ) ;
44+
4145 services . TryAddSingleton < IRabbitMQEventBus > ( options =>
4246 {
4347 IRabbitMQPersistentConnection rabbitMQPersistentConnection = options . GetRequiredService < IRabbitMQPersistentConnection > ( ) ;
4448 ILogger < DefaultRabbitMQEventBusV2 > logger = options . GetRequiredService < ILogger < DefaultRabbitMQEventBusV2 > > ( ) ;
4549 var eventBus = DefaultRabbitMQEventBusV2 . CreateInstance ( rabbitMQPersistentConnection , options , logger ) ;
4650 return eventBus ;
4751 } ) ;
48- foreach ( Type mType in typeof ( IEvent ) . GetAssemblies ( ) )
52+ foreach ( var ( registerType , handlerType , eventType , responseType ) in RabbitmqEventBusHandlers . RegisterEventResponseHandlers ( ) )
53+ {
54+ services . TryAddTransient ( registerType , handlerType ) ;
55+ }
56+ foreach ( var ( handlerType , eventType ) in RabbitmqEventBusHandlers . RegisterEventHandlers ( ) )
4957 {
50- foreach ( Type hType in typeof ( IEventHandler < > ) . GetMakeGenericType ( mType ) )
51- {
52- services . TryAddTransient ( hType ) ;
53- }
58+ services . TryAddTransient ( handlerType ) ;
5459 }
55- var responseHandlers = services . RegisterEventResponseHandlers ( ) . ToList ( ) ;
56- var serviceProvider = services . BuildServiceProvider ( ) ;
57- var _logger = serviceProvider . GetRequiredService < ILogger < DefaultRabbitMQEventBusV2 > > ( ) ;
58- var rmqeV2 = serviceProvider . GetService < IRabbitMQEventBus > ( ) ;
59- foreach ( var ( registerType , handlerType , eventType , responseType ) in responseHandlers )
60+ return services ;
61+ }
62+ /// <summary>
63+ ///
64+ /// </summary>
65+ /// <param name="app"></param>
66+ public static void UseRabbitmqEventBus ( this IApplicationBuilder app )
67+ {
68+ IRabbitMQEventBus rmqeV2 = app . ApplicationServices . GetRequiredService < IRabbitMQEventBus > ( ) ;
69+ var _logger = app . ApplicationServices . GetRequiredService < ILogger < DefaultRabbitMQEventBusV2 > > ( ) ;
70+ foreach ( var ( registerType , handlerType , eventType , responseType ) in RabbitmqEventBusHandlers . RegisterEventResponseHandlers ( ) )
6071 {
6172 rmqeV2 . Subscribe ( eventType , responseType ) ;
6273 _logger . LogInformation ( $ "subscribe:\t { eventType } \t =>\t { handlerType } <{ eventType . Name } ,{ responseType . Name } >\t return Type : \t { responseType } ") ;
6374 }
64- foreach ( Type mType in typeof ( IEvent ) . GetAssemblies ( ) )
75+ foreach ( var ( handlerType , eventType ) in RabbitmqEventBusHandlers . RegisterEventHandlers ( ) )
6576 {
66- var handlesAny = typeof ( IEventHandler < > ) . GetMakeGenericType ( mType ) ;
67- if ( handlesAny . Any ( ) )
68- {
69- rmqeV2 . Subscribe ( mType ) ;
70- foreach ( var handler in handlesAny )
71- {
72- _logger . LogInformation ( $ "subscribe:{ mType } \t =>\t { handler } \t ") ;
73- }
74- }
77+ rmqeV2 . Subscribe ( eventType ) ;
78+ _logger . LogInformation ( $ "subscribe:\t { eventType } \t =>\t { handlerType } <{ eventType . Name } >") ;
7579 }
76- return serviceProvider ;
7780 }
7881}
7982
0 commit comments