@@ -68,69 +68,69 @@ public void Publish<TMessage>(TMessage message, string exchange, string routingK
6868 _logger . WriteLog ( _persistentConnection . Configuration . Level , $ "{ DateTimeOffset . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } \t { exchange } \t { routingKey } \t { body } ") ;
6969 _eventHandlerFactory ? . PubliushEvent ( new EventBusArgs ( _persistentConnection . Endpoint , exchange , "" , routingKey , type , _persistentConnection . Configuration . ClientProvidedName , body , true ) ) ;
7070 }
71- public void Subscribe < TEvent , THandler > ( string type = ExchangeType . Topic )
72- where TEvent : IEvent
73- where THandler : IEventHandler < TEvent >
74- {
75- Subscribe ( typeof ( TEvent ) , typeof ( THandler ) ) ;
76- #region MyRegion
77- /*object attribute = typeof(TEvent).GetCustomAttributes(typeof(EventBusAttribute), true).FirstOrDefault();
78- if (attribute is EventBusAttribute attr)
79- {
80- string queue = attr.Queue ?? $"{ attr.Exchange }.{ typeof(TEvent).Name }";
81- if (!_persistentConnection.IsConnected)
82- {
83- _persistentConnection.TryConnect();
84- }
85- IModel channel;
86- #region snippet
87- try
88- {
89- channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
90- channel.QueueDeclarePassive(queue);
91- }
92- catch
93- {
94- channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
95- channel.QueueDeclare(queue: queue,
96- durable: true,
97- exclusive: false,
98- autoDelete: false,
99- arguments: null);
100- }
101- #endregion
102- channel.QueueBind(queue, attr.Exchange, attr.RoutingKey, null);
103- channel.BasicQos(0, 1, false);
104- EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
105- consumer.Received += async (model, ea) =>
106- {
107- string body = Encoding.UTF8.GetString(ea.Body);
108- bool isAck = false;
109- try
110- {
111- await ProcessEvent<TEvent, THandler>(body);
112- channel.BasicAck(ea.DeliveryTag, multiple: false);
113- isAck = true;
114- }
115- catch (Exception ex)
116- {
117- _logger.LogError(new EventId(ex.HResult), ex, ex.Message);
118- }
119- finally
120- {
121- _logger.Information($"RabbitMQEventBus\t{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}\t{isAck}\t{ea.Exchange}\t{ea.RoutingKey}\t{body}");
122- }
123- };
124- channel.CallbackException += (sender, ex) =>
125- {
71+ // public void Subscribe<TEvent, THandler>(string type = ExchangeType.Topic)
72+ // where TEvent : IEvent
73+ // where THandler : IEventHandler<TEvent>
74+ // {
75+ // // Subscribe(typeof(TEvent), typeof(THandler));
76+ // #region MyRegion
77+ // /*object attribute = typeof(TEvent).GetCustomAttributes(typeof(EventBusAttribute), true).FirstOrDefault();
78+ // if (attribute is EventBusAttribute attr)
79+ // {
80+ // string queue = attr.Queue ?? $"{ attr.Exchange }.{ typeof(TEvent).Name }";
81+ // if (!_persistentConnection.IsConnected)
82+ // {
83+ // _persistentConnection.TryConnect();
84+ // }
85+ // IModel channel;
86+ // #region snippet
87+ // try
88+ // {
89+ // channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
90+ // channel.QueueDeclarePassive(queue);
91+ // }
92+ // catch
93+ // {
94+ // channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
95+ // channel.QueueDeclare(queue: queue,
96+ // durable: true,
97+ // exclusive: false,
98+ // autoDelete: false,
99+ // arguments: null);
100+ // }
101+ // #endregion
102+ // channel.QueueBind(queue, attr.Exchange, attr.RoutingKey, null);
103+ // channel.BasicQos(0, 1, false);
104+ // EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
105+ // consumer.Received += async (model, ea) =>
106+ // {
107+ // string body = Encoding.UTF8.GetString(ea.Body);
108+ // bool isAck = false;
109+ // try
110+ // {
111+ // await ProcessEvent<TEvent, THandler>(body);
112+ // channel.BasicAck(ea.DeliveryTag, multiple: false);
113+ // isAck = true;
114+ // }
115+ // catch (Exception ex)
116+ // {
117+ // _logger.LogError(new EventId(ex.HResult), ex, ex.Message);
118+ // }
119+ // finally
120+ // {
121+ // _logger.Information($"RabbitMQEventBus\t{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}\t{isAck}\t{ea.Exchange}\t{ea.RoutingKey}\t{body}");
122+ // }
123+ // };
124+ // channel.CallbackException += (sender, ex) =>
125+ // {
126126
127- };
128- channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
129- }*/
130- #endregion
131- }
127+ // };
128+ // channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
129+ // }*/
130+ // #endregion
131+ // }
132132
133- public void Subscribe ( Type eventType , Type eventHandleType , string type = ExchangeType . Topic )
133+ public void Subscribe ( Type eventType , string type = ExchangeType . Topic )
134134 {
135135 var attributes = eventType . GetCustomAttributes ( typeof ( EventBusAttribute ) , true ) ;
136136 var millisecondsDelay = ( int ? ) _persistentConnection ? . Configuration ? . ConsumerFailRetryInterval . TotalMilliseconds ?? 1000 ;
@@ -169,7 +169,7 @@ public void Subscribe(Type eventType, Type eventHandleType, string type = Exchan
169169 bool isAck = false ;
170170 try
171171 {
172- await ProcessEvent ( body , eventType , eventHandleType , ea ) ;
172+ await ProcessEvent ( body , eventType , ea ) ;
173173 channel . BasicAck ( ea . DeliveryTag , multiple : false ) ;
174174 isAck = true ;
175175 }
@@ -222,21 +222,24 @@ public void Subscribe(Type eventType, Type eventHandleType, string type = Exchan
222222 /// <param name="eventHandleType"></param>
223223 /// <param name="args"></param>
224224 /// <returns></returns>
225- private async Task ProcessEvent ( string body , Type eventType , Type eventHandleType , BasicDeliverEventArgs args )
225+ private async Task ProcessEvent ( string body , Type eventType , BasicDeliverEventArgs args )
226226 {
227227 using ( var scope = _serviceProvider . CreateScope ( ) )
228228 {
229- object eventHandler = scope . ServiceProvider . GetRequiredService ( eventHandleType ) ;
230- if ( eventHandler == null )
229+ foreach ( Type eventHandleType in typeof ( IEventHandler < > ) . GetMakeGenericType ( eventType ) )
231230 {
232- throw new InvalidOperationException ( eventHandleType . Name ) ;
233- }
234- Type concreteType = typeof ( IEventHandler < > ) . MakeGenericType ( eventType ) ;
235- await ( Task ) concreteType . GetMethod ( nameof ( IEventHandler < IEvent > . Handle ) ) . Invoke (
236- eventHandler ,
237- new object [ ] {
231+ object eventHandler = scope . ServiceProvider . GetRequiredService ( eventHandleType ) ;
232+ if ( eventHandler == null )
233+ {
234+ throw new InvalidOperationException ( eventHandleType . Name ) ;
235+ }
236+ Type concreteType = typeof ( IEventHandler < > ) . MakeGenericType ( eventType ) ;
237+ await ( Task ) concreteType . GetMethod ( nameof ( IEventHandler < IEvent > . Handle ) ) . Invoke (
238+ eventHandler ,
239+ new object [ ] {
238240 Activator . CreateInstance ( typeof ( EventHandlerArgs < > ) . MakeGenericType ( eventType ) , new object [ ] { body , args . Redelivered , args . Exchange , args . RoutingKey } )
239- } ) ;
241+ } ) ;
242+ }
240243 }
241244 }
242245 }
0 commit comments