0

So I am trying to experiment (based on this EasyNetQ toturial: Quick Start - EasyNetQ) with a simple EasyNetQ messaging architecture involving a Publisher and a Subscriber and it doesn't seem to be working quite as expected. Both my Publisher and Subscriber are Windows Service projects in Visual Studio 2015 and the message being sent between them is an instance of a custom type (TextMessage), which is a simple Class Library that looks like this:

namespace Messaging.Messages
{
    public class TextMessage
    {
        public string Text { get; set; }
    }
}

My Publisher looks like this:

namespace Messaging.Publisher
{
    public partial class ReportService : ServiceBase
    {
        private Timer timer = null;

        public ReportService()
        {
            this.InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Library.WriteErrorLog("Report Publisher Service started");

            using (var bus = RabbitHutch.CreateBus("host=localhost"))
            {
                bus.Publish(new TextMessage
                {
                    Text = "Hello"
                });
            }
        }

        protected override void OnStop()
        {
            this.timer.Enabled = false;
            Library.WriteErrorLog("Test window service has stopped");
        }

    }
}

So nothing fancy. All it does is publish one message of type TextMessage and logs to a text file "PublisherLogFile.txt":

namespace Messaging.Publisher
{
    public static class Library
    {
        public static void WriteErrorLog(string Message)
        {
            StreamWriter sw = null;

            try
            {
                 sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\PublisherLogFile.txt", true);                      
               sw.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + ": " + Message);
                 sw.Flush();
                 sw.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

And the Subscriber looks like this:

namespace Messaging.Subscriber
{
    public partial class ReportSubscriberService : ServiceBase
    {
        public ReportSubscriberService()
        {
            this.InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            WriteErrorLog("Report Subscriber Service started");

            using (var bus = RabbitHutch.CreateBus("host=localhost"))
            {
                bus.Subscribe<TextMessage>("testId", HandleTextMessage);
            }
        }

        protected override void OnStop()
        {
            WriteErrorLog("Exiting Report Subscriber Service");
        }

        private static void HandleTextMessage(TextMessage textMessage)
        {
            WriteErrorLog("Got message: " + textMessage.Text);
        }

        private static void WriteErrorLog(string Message)
        {
            try
            {
                var sw = new     StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\SubscriberLogFile.txt", true);
                sw.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + ": " + Message);
                sw.Flush();
                sw.Close();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

Also very simple. All it does is receive messages of type TextMessage and prints out the value of their Text attribute to a log file "SubscriberLogFile.txt". The problem is that it doesn't seem to be receiving the message because it doesn't log to the text file above. It looks like the HandleTextMessage handler in my Suscriber is never called. This is the content of "SubscriberLogFile.txt":

SubscriberLogFile content

Also, looking at the RabbitMQ management console, no connections or channels are created, just one queue:

RabbitMQ Management console overview

RabbitMQ Management console queues

And the RabbitMQ log:

RabbitMQ log

When I first did the same experiment, but with the difference that the Publisher and Subscriber were Console Applications instead of Windows Services, things seemed to work fine. What could be the problem here?

lukegf
  • 2,147
  • 3
  • 26
  • 39
  • What do you see if you restart publisher service multiple times, while receiver service is running? – Evk May 18 '16 at 14:41
  • What I am noticing is that the number of messages in the Queues view of the RabbitMQ management console increases by one each time, but the SubscriberLogFile still doesn't print out the message content. However, I think the problem maybe that I am disposing the bus (including connections and subscriptions) immediately after opening it, in my Publisher. That was the other difference with the RabbitMQ tutorial. There the bus stayed open until the user typed 'Quit' in the command line. – lukegf May 18 '16 at 14:52
  • 1
    Yes indeed - dispose bus only in service OnStop event. – Evk May 18 '16 at 14:53

1 Answers1

1

The problem was that I was disposing the bus as soon as I opened it. Instead it has to stay open and only be disposed when the service is stopped, on the OnStop event.

lukegf
  • 2,147
  • 3
  • 26
  • 39