繁体   English   中英

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

[英]Why and how RabbitMQ .NET Core client causes the application shutdown

我按如下方式创建客户端( 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调用结束,好像我不了解如何组织防弹接收。

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

根据RabbitMQ文档 ,我不应该退出Main方法。 他们使用ReadLine ,但我更喜欢按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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM