|
public class RPCClient
{
private IConnection connection;
private IModel channel;
private string replyQueueName;
private QueueingBasicConsumer consumer;
public RPCClient()
{
try
{
var factory = new ConnectionFactory()
{
HostName = "192.168.10.198",
UserName = "123",
Password = "123",
Port = 5672,
RequestedHeartbeat = 5,
AutomaticRecoveryEnabled = true
};
connection = factory.CreateConnection();
channel = connection.CreateModel();
replyQueueName = channel.QueueDeclare().QueueName;
consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queue: replyQueueName,
noAck:true,
consumer: consumer);
}
catch (Exception ex)
{
}
}
public string GetRPCResult()
{
try
{
var corrId = Guid.NewGuid().ToString();
var props = channel.CreateBasicProperties();
props.ReplyTo = replyQueueName;
props.CorrelationId = corrId;
var messageBytes = Encoding.UTF8.GetBytes("66666");
channel.BasicPublish(exchange: "",
routingKey: "rpc_queue33",
basicProperties: props,
body: messageBytes);
while (true)
{
var ea = consumer.Queue.Dequeue();
if (ea.BasicProperties.CorrelationId == corrId)
{
Console.WriteLine(Encoding.UTF8.GetString(ea.Body));
return Encoding.UTF8.GetString(ea.Body);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return "";
}
}
执行到:var ea = consumer.Queue.Dequeue(); 会报异常,在本地是正常的,上服务器就报这个错,求指导:
"System.IO.EndOfStreamException: SharedQueue closed\n at RabbitMQ.Util.SharedQueue`1[T].EnsureIsOpen () [0x00008] in <8ebd6ca48b534a1a91b39f38e33ac7be>:0 \n at RabbitMQ.Util.SharedQueue`1[T].Dequeue () [0x00013] in <8ebd6ca48b534a1a91b39f38e33ac7be>:0
|
|