@@ -7,57 +7,17 @@ public static class ServiceCollectionExtensions
77 /// 添加RabbitMQEventBus
88 /// </summary>
99 /// <param name="services"></param>
10- /// <param name="connectionAction">使用匿名函数取得连接字符串,用来兼容使用Consul获取服务地址的情况</param>
10+ /// <param name="endpoint"></param>
11+ /// <param name="port"></param>
12+ /// <param name="username"></param>
13+ /// <param name="password"></param>
14+ /// <param name="visualHost"></param>
1115 /// <param name="eventBusOptionAction"></param>
16+ /// <param name="moduleOptions"></param>
1217 /// <returns></returns>
13- //public static IServiceCollection AddRabbitMQEventBus(this IServiceCollection services, Func<string> connectionAction, Action<RabbitMQEventBusConnectionConfigurationBuild> eventBusOptionAction)
14- //{
15- // RabbitMQEventBusConnectionConfiguration configuration = new();
16- // RabbitMQEventBusConnectionConfigurationBuild configurationBuild = new(configuration);
17- // eventBusOptionAction?.Invoke(configurationBuild);
18- // services.TryAddSingleton<IRabbitMQPersistentConnection>(options =>
19- // {
20- // ILogger<DefaultRabbitMQPersistentConnection> logger = options.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
21- // var connection = new DefaultRabbitMQPersistentConnection(configuration, connectionAction, logger);
22- // connection.TryConnect();
23- // Console.WriteLine("创建一次连接");
24- // return connection;
25- // });
26- // services.TryAddSingleton<IEventHandlerModuleFactory, EventHandlerModuleFactory>();
27- // services.TryAddSingleton<IRabbitMQEventBus, DefaultRabbitMQEventBus>();
28- // foreach (Type mType in typeof(IEvent).GetAssemblies())
29- // {
30- // services.TryAddTransient(mType);
31- // foreach (Type hType in typeof(IEventHandler<>).GetMakeGenericType(mType))
32- // {
33- // services.TryAddTransient(hType);
34- // }
35- // }
36- // return services;
37- //}
38- /// <summary>
39- /// 自动订阅
40- /// </summary>
41- /// <param name="app"></param>
42- //public static void RabbitMQEventBusAutoSubscribe(this IApplicationBuilder app)
43- //{
44- // IRabbitMQEventBus eventBus = app.ApplicationServices.GetRequiredService<IRabbitMQEventBus>();
45- // ILogger<IRabbitMQEventBus> logger = app.ApplicationServices.GetRequiredService<ILogger<IRabbitMQEventBus>>();
46- // using (logger.BeginScope("EventBus Subscribe"))
47- // {
48- // logger.LogInformation($"=======================================================================");
49- // foreach (Type mType in typeof(IEvent).GetAssemblies())
50- // {
51- // var handlesAny = typeof(IEventHandler<>).GetMakeGenericType(mType);
52- // if (handlesAny.Any())
53- // {
54- // logger.LogInformation($"{mType.Name}\t=>\t{string.Join("、", handlesAny)}");
55- // eventBus.Subscribe(mType);
56- // }
57- // }
58- // logger.LogInformation($"=======================================================================");
59- // }
60- //}
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 )
19+ => AddRabbitMQEventBus ( services , ( ) => $ "amqp://{ username } :{ password } @{ endpoint } :{ port } /{ visualHost } ", eventBusOptionAction , moduleOptions ) ;
20+
6121 /// <summary>
6222 /// 添加RabbitMQEventBus
6323 /// </summary>
@@ -96,10 +56,10 @@ public static IServiceProvider AddRabbitMQEventBus(this IServiceCollection servi
9656 var serviceProvider = services . BuildServiceProvider ( ) ;
9757 var _logger = serviceProvider . GetRequiredService < ILogger < DefaultRabbitMQEventBusV2 > > ( ) ;
9858 var rmqeV2 = serviceProvider . GetService < IRabbitMQEventBus > ( ) ;
99- foreach ( var handler in responseHandlers )
59+ foreach ( var ( registerType , handlerType , eventType , responseType ) in responseHandlers )
10060 {
101- rmqeV2 . Subscribe ( handler . eventType , handler . responseType ) ;
102- _logger . LogInformation ( $ "subscribe:\t { handler . eventType } \t =>\t { handler . handlerType } <{ handler . eventType . Name } ,{ handler . responseType . Name } >\t return Type : \t { handler . responseType } ") ;
61+ rmqeV2 . Subscribe ( eventType , responseType ) ;
62+ _logger . LogInformation ( $ "subscribe:\t { eventType } \t =>\t { handlerType } <{ eventType . Name } ,{ responseType . Name } >\t return Type : \t { responseType } ") ;
10363 }
10464 foreach ( Type mType in typeof ( IEvent ) . GetAssemblies ( ) )
10565 {
0 commit comments