Search code examples
c#msmqmulticastmsmq-wcf

MSMQ Message Multicast: Unable to receive messages on different machine


I want to create Publisher and Subscriber Model using MSMQ Multicast feature. I have already followed the answer in the link without success MSMQ - Cannot receive from Multicast queues Messages getting sent and received in local machine.

Sender:

using (var helloQueue = new MessageQueue("FormatName:MULTICAST=234.1.1.1:8001"))
{
    while (true)
    {
        var stopWatch = new Stopwatch();
        stopWatch.Start();

        for (var i = 0; i < 1000; i++)
        {
            SendMessage(helloQueue,
                string.Format("{0}: msg:{1} hello world ", DateTime.UtcNow.Ticks, i));
        }

        stopWatch.Stop();
        Console.ReadLine();

        Console.WriteLine("====================================================");
        Console.WriteLine("[MSMQ] done sending 1000 messages in " + stopWatch.ElapsedMilliseconds);
        Console.WriteLine("[MSMQ] Sending reset counter to consumers.");

        SendMessage(helloQueue, "reset");
        Console.ReadLine();
    }
}

Receiver:

int messagesReceived = 0;
var messages = new Queue<string>(5000);
var filePath = typeof(Subscriber).FullName + ".txt";
var path = @".\private$\hello-queue";

using (var helloQueue = new MessageQueue(path))
{
    helloQueue.MulticastAddress = "234.1.1.1:8001";
    while (true)
    {
        var message = helloQueue.Receive();
        if (message == null)
            return;

        var reader = new StreamReader(message.BodyStream);
        var body = reader.ReadToEnd();

        messagesReceived += 1;

        messages.Enqueue(body);
        Console.WriteLine(" [MSMQ] {0} Received {1}", messagesReceived, body);

        if (string.CompareOrdinal("reset", body) == 0)
        {
            messagesReceived = 0;
            File.WriteAllText(filePath, body);
            messages.Clear();
        }
    }
}

I added key in registry for multicastbind with IP showing in event log(not sure about this). The MulticastAddress that we specify in queue, is it something specific or we can use anything in the range specified ?


Solution

  • This got resolved by just changing the port number. Rest was fine.