44using RabbitMQ . Client ;
55using RabbitMQ . Client . Events ;
66using RabbitMQ . EventBus . AspNetCore . Attributes ;
7+ using RabbitMQ . EventBus . AspNetCore . Configurations ;
78using RabbitMQ . EventBus . AspNetCore . Events ;
89using RabbitMQ . EventBus . AspNetCore . Factories ;
910using RabbitMQ . EventBus . AspNetCore . Modules ;
@@ -68,68 +69,6 @@ public void Publish<TMessage>(TMessage message, string exchange, string routingK
6869 _logger . WriteLog ( _persistentConnection . Configuration . Level , $ "{ DateTimeOffset . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } \t { exchange } \t { routingKey } \t { body } ") ;
6970 _eventHandlerFactory ? . PubliushEvent ( new EventBusArgs ( _persistentConnection . Endpoint , exchange , "" , routingKey , type , _persistentConnection . Configuration . ClientProvidedName , body , true ) ) ;
7071 }
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- // {
126-
127- // };
128- // channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
129- // }*/
130- // #endregion
131- //}
132-
13372 public void Subscribe ( Type eventType , string type = ExchangeType . Topic )
13473 {
13574 var attributes = eventType . GetCustomAttributes ( typeof ( EventBusAttribute ) , true ) ;
@@ -138,7 +77,7 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
13877 {
13978 if ( attribute is EventBusAttribute attr )
14079 {
141- string queue = attr . Queue ?? $ "{ attr . Exchange } .{ eventType . Name } ";
80+ string queue = attr . Queue ?? ( _persistentConnection . Configuration . Prefix == QueuePrefixType . ExchangeName ? $ "{ attr . Exchange } .{ eventType . Name } " : _persistentConnection . Configuration . ClientProvidedName ) ;
14281 if ( ! _persistentConnection . IsConnected )
14382 {
14483 _persistentConnection . TryConnect ( ) ;
@@ -152,8 +91,9 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
15291 }
15392 catch
15493 {
94+
15595 channel = _persistentConnection . ExchangeDeclare ( exchange : attr . Exchange , type : type ) ;
156- channel . QueueDeclare ( queue : queue ,
96+ channel . QueueDeclare ( queue : queue , //_persistentConnection.Configuration.ClientProvidedName
15797 durable : true ,
15898 exclusive : false ,
15999 autoDelete : false ,
@@ -199,24 +139,6 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
199139 /// <summary>
200140 ///
201141 /// </summary>
202- /// <typeparam name="TEvent"></typeparam>
203- /// <typeparam name="TEventHandle"></typeparam>
204- /// <param name="body"></param>
205- /// <returns></returns>
206- //private async Task ProcessEvent<TEvent, TEventHandle>(string body)
207- // where TEvent : IEvent
208- // where TEventHandle : IEventHandler<TEvent>
209- //{
210- // using (var scope = _serviceProvider.CreateScope())
211- // {
212- // TEventHandle eventHandler = scope.ServiceProvider.GetRequiredService<TEventHandle>();
213- // TEvent integrationEvent = JsonConvert.DeserializeObject<TEvent>(body);
214- // await eventHandler.Handle(integrationEvent/*, new MessageEventArgs(body, false)*/);
215- // }
216- //}
217- /// <summary>
218- ///
219- /// </summary>
220142 /// <param name="body"></param>
221143 /// <param name="eventType"></param>
222144 /// <param name="eventHandleType"></param>
0 commit comments