为什么和如何RabbitMQ .NET Core客户端导致应用程序关闭

时间:2018-12-27 11:08:40

标签: c# .net-core rabbitmq

我按如下方式创建客户端(exchangeAndQueue2是持久的并且存在):

var factory = new ConnectionFactory() {
    HostName = hostname,
    Port = port,
    UserName = userName,
    Password = password
};
factory.AutomaticRecoveryEnabled = true;
factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10);
connection = factory.CreateConnection();
channel = connection.CreateModel();
channel.ExchangeDeclare(exchange1, ExchangeType.Fanout, durable: true);
consumer = new EventingBasicConsumer(channel);
consumer.Registered += (s, e) => { Trace.TraceInformation("Consumer Registered"); };
consumer.ConsumerCancelled += (s, e) => { Trace.TraceInformation("Consumer Cancelled"); };
consumer.Received += NewMessage;
channel.BasicConsume(exchangeAndQueue2, autoAck: false, consumer: consumer);

在大多数情况下,它可以正常工作,但是有时候,由于RabbitMQ,我的整个应用程序都无提示地关闭了。我订阅了FirstChanceExceptionProcessExit事件

AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
    Trace.TraceWarning("Application is shutting down...");
    mq.CloseConnection();
}

这就是我设法抓住的地方:

Server Error: 0 : System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
Server Error: 0 : System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
Server Error: 0 : System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at RabbitMQ.Client.Impl.InboundFrame.ReadFrom(NetworkBinaryReader reader)
Server Information: 0 : RabbitMQ Consumer Cancelled
Server Warning: 0 : Application is shutting down...

可能是什么,以及如何调试这些东西?库甚至有可能关闭整个应用程序? NewMessage处理程序的整个代码包装在try/catch (Exception ex) ...

服务器版本3.1.5,客户端版本5.1.0

我的Main方法以channel.BasicConsume调用结束,好像我不明白如何组织防弹接收。

我猜想应用程序在接收方线程退出时退出...如何正确重生它?

1 个答案:

答案 0 :(得分:0)

根据RabbitMQ documentation,我不应该退出Main方法。他们使用ReadLine,但我更喜欢像this answer中那样捕捉Ctrl + C:

static void Main(string[] args) {
    //...
    channel.BasicConsume(exchangeAndQueue2, autoAck: false, consumer: consumer);
    Trace.TraceInformation("Application started. Press Ctrl+C to shut down.");
    var exitEvent = new AutoResetEvent(false);
    Console.CancelKeyPress += (s, e) => { e.Cancel = true; exitEvent.Set(); };
    exitEvent.WaitOne();
    Trace.TraceInformation("Ctrl+C pressed.");
    mq.CloseConnection();
}