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












0














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?










share|improve this question
























  • 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
















0














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?










share|improve this question
























  • 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














0












0








0







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Himura

1418




1418












  • 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


















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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();
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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();
    }





    share|improve this answer




























      0














      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();
      }





      share|improve this answer


























        0












        0








        0






        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();
        }





        share|improve this answer














        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();
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        Himura

        1418




        1418






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas