示例#1
0
 private static ConnectionFactory GenerateConnectionFactory(RabbitMQConfigurations configurations)
 {
     return(new ConnectionFactory()
     {
         HostName = configurations.HostName,
         Port = configurations.Port,
         UserName = configurations.UserName,
         Password = configurations.Password
     });
 }
示例#2
0
        public static void Main()
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile($"appsettings.json");

            _configuration = builder.Build();

            var rabbitMQConfigurations = new RabbitMQConfigurations();

            new ConfigureFromConfigurationOptions <RabbitMQConfigurations>(
                _configuration.GetSection("RabbitMQConfigurations"))
            .Configure(rabbitMQConfigurations);

            var factory = GenerateConnectionFactory(rabbitMQConfigurations);

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();
            DeclareQueue(channel);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += ConsumerReceived;
            channel.BasicConsume(queue: "Messages",
                                 autoAck: true,
                                 consumer: consumer);

            Console.WriteLine("Waiting new messages for processment");

            Console.CancelKeyPress += (o, e) =>
            {
                Console.WriteLine("Going out");

                _waitHandle.Set();
                e.Cancel = true;
            };

            _waitHandle.WaitOne();
        }