.NET RabbitMQ发送和接收

时间:2017-01-18 08:09:04

标签: c# android asp.net rabbitmq

我正在开发两个在RabbitMQ之间连接的.NET应用程序。我尝试过使用rabbitmq web的例子,但是没有用。

我有另一个Android应用程序,它接收来自.NET应用程序与RabbitMQ的消息,但两个.NET应用程序之间无法正常工作。

我有两个不同的项目,一个要发送,另一个要接收。

Send.cs

string nombreCola = "Test";
        var factory = new ConnectionFactory();
        factory.Uri = "amqp://user:pass@ip:5672/NameVirtualHost";
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: nombreCola,
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string message = mensaje;
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: nombreCola,
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

Receive.cs

string nombreCola = "Test";
        var factory = new ConnectionFactory();
        factory.Uri = "amqp://user:ip:5672/NameVirtualHost";
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: nombreCola,
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
                label1.Text = message;
            };
            channel.BasicConsume(queue: nombreCola,
                                 noAck: true,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

0 个答案:

没有答案