Example #1
0
        private static void sendNameMessage(string nameMessage, IConfiguration configuration)
        {
            var             hostName         = configuration.GetSection("RabbitMQConnectionString")["HostName"];
            var             userName         = configuration.GetSection("RabbitMQConnectionString")["UserName"];
            var             password         = configuration.GetSection("RabbitMQConnectionString")["Password"];
            var             messageQueueName = configuration.GetSection("QueueDetails")["Queue"];
            RabbitMQService rabbitMQService  = new RabbitMQService(hostName, userName, password);

            rabbitMQService.MessageQueueName = messageQueueName;
            try
            {
                var connection = rabbitMQService.GetConnection();
                var channel    = connection.CreateModel();
                channel.QueueDeclare(queue: rabbitMQService.MessageQueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                var body = Encoding.UTF8.GetBytes(nameMessage);
                channel.BasicPublish(exchange: "", routingKey: rabbitMQService.MessageQueueName, mandatory: true, basicProperties: null, body: body);
                Console.WriteLine("message published");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #2
0
        private static void PrintReceivedNameMessage(IConfigurationRoot configuration)
        {
            var             hostName         = configuration.GetSection("RabbitMQConnectionString")["HostName"];
            var             userName         = configuration.GetSection("RabbitMQConnectionString")["UserName"];
            var             password         = configuration.GetSection("RabbitMQConnectionString")["Password"];
            var             messageQueueName = configuration.GetSection("QueueDetails")["Queue"];
            RabbitMQService rabbitMQService  = new RabbitMQService(hostName, userName, password);

            rabbitMQService.MessageQueueName = messageQueueName;
            try
            {
                var connection = rabbitMQService.GetConnection();
                var channel    = connection.CreateModel();
                channel.QueueDeclare(queue: rabbitMQService.MessageQueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += Consumer_Received;
                channel.BasicConsume(queue: rabbitMQService.MessageQueueName, autoAck: false, consumer: consumer);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }