public void SubscribeToQueue(InterserviceQueueTypeEnum queueType) { _logger.Information($"Subscribing to queue with type '{queueType}'..."); if (_channelModel == null) { var connection = _rabbitConnectionService.GetConnection(); _channelModel = connection.CreateModel(); } var consumer = new EventingBasicConsumer(_channelModel); consumer.Received += async(model, eventArgs) => { try { await ProcessReceivedMessageAsync(model, eventArgs); } catch (Exception exception) { _logger.Write(exception); } }; var queueDeclareResult = _channelModel.QueueDeclare(queueType); _channelModel.BasicConsume(queueDeclareResult.QueueName, true, consumer); _logger.Information($"Successfully subscribed to queue with type '{queueType}'"); }
public void PublishMessage(InterserviceQueueTypeEnum queueType, IInterserviceMessageModel messageModel) { _logger.Information($"Publishing interservice message to queue with type = '{queueType}'..."); _logger.Debug($"Interservice message = '{messageModel.ToJson()}'"); var connection = _rabbitConnectionService.GetConnection(); using var channelModel = connection.CreateModel(); var queueDeclareResult = channelModel.QueueDeclare(queueType); var messageJson = messageModel.ToJson(); var messageBytes = Encoding.UTF8.GetBytes(messageJson); channelModel.BasicPublish("", queueDeclareResult.QueueName, null, messageBytes); _logger.Information($"Interservice message was successfully published to queue with type = '{queueType}'"); }