Reputation: 41
Here is my code for MassTransit.
The subscriber console app:
namespace ConsoleSubscriber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("set up a subscriber");
Bus.Initialize(config =>
{
config.UseMsmq();
config.VerifyMsmqConfiguration();
config.UseMulticastSubscriptionClient();
config.UseControlBus();
config.ReceiveFrom("msmq://localhost/test_end");
config.Subscribe(s => s.Instance(new Consumer()).Permanent());
});
Console.Read();
}
}
public class Consumer : Consumes<Message>.All
{
public void Consume(Message message)
{
Console.WriteLine("start consuming message");
if (string.IsNullOrEmpty(message.Ids))
{
Console.WriteLine("no ids");
}
else
{
Console.WriteLine("ids are " + message.Ids);
}
}
}
}
Message class:
[Serializable]
public class Message
{
public string Ids { get; set; }
}
The publisher console app:
namespace ConsolePublisher
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("start publishing");
// initialize bus
Bus.Initialize(config =>
{
config.UseMsmq();
config.VerifyMsmqConfiguration();
config.UseMulticastSubscriptionClient();
config.UseControlBus();
config.ReceiveFrom("msmq://localhost/test_queue");
});
Bus.Instance.Publish(new Message());
Console.WriteLine("Message sent 1");
var msg2 = new Message() { Ids ="1,2,3,4" };
Bus.Instance.Publish(msg2);
Console.WriteLine("Message sent 2");
Console.Read();
}
}
}
When I run these two console applications, it seems the messages are not picked up by the consumer. Any ideas?
Cheers!
Upvotes: 4
Views: 1418
Reputation: 10547
Try putting a sleep in before calling publish. Multicast subscriptions can take a moment to get configured as the two instances are talking to each other. Also ensure you started the consumer before the publisher. Also replacing config.Subscribe(s => s.Instance(new Consumer()).Permanent());
with config.Subscribe<Consumer>();
might help.
There are other routing methods that can keep the subscription data between restarts of the process once you move forward. I would start to look at using the Subscription Service, static routing, or just using RabbitMQ once you get a handle on this.
Upvotes: 6