Why and how RabbitMQ .NET Core client causes the application shutdown
I create the client as follows (the exchangeAndQueue2
are durable and exist):
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);
Most of the time it works fine, but SOMETIMES my whole application shuts down silently because of the RabbitMQ. I subscribed to the FirstChanceException
and ProcessExit
events
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
Trace.TraceWarning("Application is shutting down...");
mq.CloseConnection();
}
And here's what I managed to catch:
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...
What could it be and how to debug such things? How is it even possible that a library shuts down the whole app? The whole code of the NewMessage
handler is wrapped in try/catch (Exception ex)
...
Server version 3.1.5, Client version 5.1.0
My Main
method ends with the channel.BasicConsume
call, looks like I don't understand how to organize a bulletproof reception.
I guess the application exits when the receiver thread quits... How to properly respawn it?
c# .net-core rabbitmq
add a comment |
I create the client as follows (the exchangeAndQueue2
are durable and exist):
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);
Most of the time it works fine, but SOMETIMES my whole application shuts down silently because of the RabbitMQ. I subscribed to the FirstChanceException
and ProcessExit
events
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
Trace.TraceWarning("Application is shutting down...");
mq.CloseConnection();
}
And here's what I managed to catch:
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...
What could it be and how to debug such things? How is it even possible that a library shuts down the whole app? The whole code of the NewMessage
handler is wrapped in try/catch (Exception ex)
...
Server version 3.1.5, Client version 5.1.0
My Main
method ends with the channel.BasicConsume
call, looks like I don't understand how to organize a bulletproof reception.
I guess the application exits when the receiver thread quits... How to properly respawn it?
c# .net-core rabbitmq
Please show us the content of theNewMessage
method.
– Ian Kemp
2 days ago
1
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago
add a comment |
I create the client as follows (the exchangeAndQueue2
are durable and exist):
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);
Most of the time it works fine, but SOMETIMES my whole application shuts down silently because of the RabbitMQ. I subscribed to the FirstChanceException
and ProcessExit
events
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
Trace.TraceWarning("Application is shutting down...");
mq.CloseConnection();
}
And here's what I managed to catch:
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...
What could it be and how to debug such things? How is it even possible that a library shuts down the whole app? The whole code of the NewMessage
handler is wrapped in try/catch (Exception ex)
...
Server version 3.1.5, Client version 5.1.0
My Main
method ends with the channel.BasicConsume
call, looks like I don't understand how to organize a bulletproof reception.
I guess the application exits when the receiver thread quits... How to properly respawn it?
c# .net-core rabbitmq
I create the client as follows (the exchangeAndQueue2
are durable and exist):
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);
Most of the time it works fine, but SOMETIMES my whole application shuts down silently because of the RabbitMQ. I subscribed to the FirstChanceException
and ProcessExit
events
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
Trace.TraceWarning("Application is shutting down...");
mq.CloseConnection();
}
And here's what I managed to catch:
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...
What could it be and how to debug such things? How is it even possible that a library shuts down the whole app? The whole code of the NewMessage
handler is wrapped in try/catch (Exception ex)
...
Server version 3.1.5, Client version 5.1.0
My Main
method ends with the channel.BasicConsume
call, looks like I don't understand how to organize a bulletproof reception.
I guess the application exits when the receiver thread quits... How to properly respawn it?
c# .net-core rabbitmq
c# .net-core rabbitmq
edited 2 days ago
asked 2 days ago
Himura
1418
1418
Please show us the content of theNewMessage
method.
– Ian Kemp
2 days ago
1
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago
add a comment |
Please show us the content of theNewMessage
method.
– Ian Kemp
2 days ago
1
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago
Please show us the content of the
NewMessage
method.– Ian Kemp
2 days ago
Please show us the content of the
NewMessage
method.– Ian Kemp
2 days ago
1
1
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
According to the RabbitMQ documentation, I should not exit the Main
method. They use ReadLine
, but I prefer catching Ctrl+C like in this answer:
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();
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944054%2fwhy-and-how-rabbitmq-net-core-client-causes-the-application-shutdown%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to the RabbitMQ documentation, I should not exit the Main
method. They use ReadLine
, but I prefer catching Ctrl+C like in this answer:
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();
}
add a comment |
According to the RabbitMQ documentation, I should not exit the Main
method. They use ReadLine
, but I prefer catching Ctrl+C like in this answer:
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();
}
add a comment |
According to the RabbitMQ documentation, I should not exit the Main
method. They use ReadLine
, but I prefer catching Ctrl+C like in this answer:
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();
}
According to the RabbitMQ documentation, I should not exit the Main
method. They use ReadLine
, but I prefer catching Ctrl+C like in this answer:
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();
}
edited 2 days ago
answered 2 days ago
Himura
1418
1418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944054%2fwhy-and-how-rabbitmq-net-core-client-causes-the-application-shutdown%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please show us the content of the
NewMessage
method.– Ian Kemp
2 days ago
1
It's a long operation inside try/catch with Acknowledgement or Rejection in the end. Anyway, I think I figured out what's wrong and going to post the answer
– Himura
2 days ago