Ejemplo n.º 1
0
        /// <summary>
        /// Отправить сообщение в RabbitMQ. (Добавить сообщение в очередь).
        /// </summary>
        /// <param name="message">Сообщение.</param>
        /// <param name="cancellationToken">Токен аннулирования. Не используется.</param>
        /// <returns><see cref="Task"/> объект асинхронного отправки сообщения.</returns>
        public Task SendMessageAsync(TMessage message, CancellationToken cancellationToken)
        {
            var factory = RabbitConnectionFactory.CreateFactory(configuration);

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(configuration.ExchangeName, ExchangeType.Direct);
                    channel.BasicPublish(configuration.ExchangeName, configuration.Routingkey, null, SerializeMessage(message));
                }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        private void CreateConnection()
        {
            logger.LogInformation("RabbitMQ configurations from appsettings. " +
                                  $"Host: {configuration.Host}, " +
                                  $"UserName: {configuration.UserName}, " +
                                  $"ExchangeName:{configuration.ExchangeName}, " +
                                  $"QueueName:{configuration.QueueName} " +
                                  $"Routingkey: {configuration.Routingkey}");

            var factory = RabbitConnectionFactory.CreateFactory(configuration);

            logger.LogInformation("Try to create connection to Rabbit MQ.");

            connection = factory.CreateConnection();

            logger.LogInformation("Created connection to Rabbit MQ.");
        }