using System; using System.Threading; using EasyNetQ; using EasyNetQ.ConnectionString; using EasyNetQ.Loggers; namespace EasyNetQBug { public class Program { public static void Main(String[] args) { AppDomain.CurrentDomain.UnhandledException += (sender, a) => { Console.WriteLine("Fatal Exception:\n" + a.ExceptionObject); }; var client = new RabbitClient(); client.Start(); // simulate long-running process while (true) { Thread.Sleep(5000); } } } public class RabbitClient { private IBus bus; private IDisposable responder; public void Start() { var config = ConnectionConfiguration(); bus = RabbitHutch.CreateBus(config, RegisterConsoleLogger); if (bus.IsConnected) { RegisterResponder(); } bus.Connected += RegisterResponder; bus.Disconnected += DeregisterResponder; } private void RegisterConsoleLogger(IServiceRegister serviceRegister) { serviceRegister.Register(serviceProvider => new ConsoleLogger()); } private static ConnectionConfiguration ConnectionConfiguration() { var config = new ConnectionStringParser().Parse("host=localhost"); config.VirtualHost = "/"; config.PublisherConfirms = true; config.PersistentMessages = true; config.Timeout = 5; return config; } private void RegisterResponder() { try { // If rabbit loses connection here, and this occurs twice, // EasyNetQ throws an exception from within a thread, // and the application cannot recover. responder = bus.Respond(Respond); } catch (Exception e) { Console.WriteLine("An error occurred while responding: " + e); } } private void DeregisterResponder() { if (responder == null) return; responder.Dispose(); responder = null; } private static string Respond(string arg) { return arg + "."; } } }