protected override void OnConnect(object sender, EventArgs e) { if (RabbitMqConnection.RegisteredConnectors.ContainsKey(Id)) { return; } IModel channel = RabbitMqConnection.CreateChannel(this); RabbitMqConnection.ConfigureChannel(channel, BrokerConfiguration.ChannelConfiguration); _eventingBasicConsumer = RabbitMqConnection.CreateEventingBasicConsumer(channel); _eventingBasicConsumer.Received += EventingBasicConsumerOnReceived; foreach (BrokerSubscriberConfiguration config in BrokerConfiguration.BrokerSubscriberConfiguration) { RabbitMqConnection.CreateExchange(channel, config.ExchangeName, config.ExchangeType); config.QueueName = RabbitMqConnection.CreateQueue(channel, config.QueueName, config.Exclusive, config.Durable, config.AutoDelete); switch (config.ExchangeType) { case ExchangeType.Topic: case ExchangeType.Direct: if (config.Topic != null) { foreach (string topic in config.Topic) { RabbitMqConnection.CreateBinding(channel, config.QueueName, config.ExchangeName, topic); } } else { foreach (KeyValuePair <Type, List <Action <object, MessageMetadata> > > obj in _registerMethodDictionary) { if (!obj.Key.IsGenericType) { RabbitMqConnection.CreateBinding(channel, config.QueueName, config.ExchangeName, obj.Key.Name); } } } break; case ExchangeType.Fanout: RabbitMqConnection.CreateBinding(channel, config.QueueName, config.ExchangeName); break; } RabbitMqConnection.ConsumeQueue(this, channel, config.QueueName, Id.ToString(), true, _eventingBasicConsumer); } }
public Task <Tuple <TResponse, MessageMetadata, string> > RequestAsync <TResponse>(IMessage message, MessageMetadata metadata) where TResponse : class, IMessage { string queueName = null; try { var taskCompletionSource = new TaskCompletionSource <Tuple <TResponse, MessageMetadata, string> >(); string routingKey = string.IsNullOrEmpty(metadata.ReplyTo) ? string.IsNullOrEmpty(metadata.Topic) ? message.GetType().Name : metadata.Topic : metadata.ReplyTo; IModel channel = RabbitMqConnection.CreateChannel(this); queueName = RabbitMqConnection.CreateQueue(channel, string.Empty, true, false, false); metadata.MessageId = string.IsNullOrEmpty(metadata.ReplyTo) ? queueName : metadata.ReplyTo; metadata.ReplyTo = queueName; RabbitMqConnection.CreateBinding(channel, queueName, MessageMetadata.RpcDestination, metadata.ReplyTo); EventingBasicConsumer consumer = RabbitMqConnection.CreateEventingBasicConsumer(channel); consumer.Received += ConsumerOnReceived; RabbitMqConnection.ConsumeQueue(this, channel, queueName, string.Empty, true, consumer); IBasicProperties basicProperties = RabbitMqConnection.CreateBasicProperties(channel, message, metadata); basicProperties.ReplyTo = string.IsNullOrEmpty(metadata.ReplyTo) ? queueName : metadata.ReplyTo; if (!basicProperties.IsHeadersPresent()) { basicProperties.Headers = new Dictionary <string, object>(); } basicProperties.Headers[MessageMetadata.ResponseDestinationHeaderKey] = MessageMetadata.RpcDestination; RegisterResponseAction(basicProperties.CorrelationId, queueName, consumer, typeof(TResponse), taskCompletionSource); byte[] body = SerializeMessage(message, metadata.SerializationType); Logger.LogInformation( $"Send request to: {metadata.Destination} with routingkey: {routingKey} - ReplyTo: {basicProperties.ReplyTo} of type: {typeof(TResponse).Name} id: {basicProperties.CorrelationId}"); RabbitMqConnection.PublishMessage(channel, metadata.Destination, basicProperties, body, routingKey); return(taskCompletionSource.Task); } catch (Exception e) { Logger.LogError(e, $"{nameof(RequestAsync)}"); if (queueName != null) { ForgetMessage(queueName); } throw; } }