我想知道我一次只能收到一条消息 这是
的基本代码var factory = new ConnectionFactory() { HostName = "localhost" };
var connection = factory.CreateConnection()
var channel = connection.CreateModel()
channel.QueueDeclare("hello", false, false, false, null);
var consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("hello", true, consumer);
BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Response.Write(message + " Received.");
答案 0 :(得分:10)
如果您要等待1条消息,请继续使用channel.BasicConsume
,但请在单条消息后留下消费者方法。
如果您只收到1条消息(如果队列中至少有1条消息),那么channel.BasicGet
var data = channel.BasicGet(queueName, true);
P.S:
CloudAMQP with .NET: Getting started页上有很好的例子。
答案 1 :(得分:3)
如果您需要转换消息中的文本,请补充上一条消息:
var data = channel.BasicGet(QueueName, true);
var message = System.Text.Encoding.UTF8.GetString(data.Body);