My application consists of an ASP.NET MVC 3 application and a Windows Service, both on the same box. The MVC application uses MassTransit to place messages on an MSMQ queue. The service uses MassTransit to read these messages from MSMQ and perform some work. When I load up the service and the application and have them running simultaneously, everything seems to work fine. However, if I load only the web application, my messages are not being added to MSMQ and essentially disappear. The odd thing is that if i load up my web application and then start and immediately stop said service, subsequent messages sent from the web app are added to the queue.
There weren't many good/modern examples of using MassTransit with StructureMap and/or MVC, so it could be a problem with my configuration, but after a couple of hours of trial and error I am not sure how to proceed. I'm also may be misusing MassTransit - really what I need is a fire and forget solution from the web application...
My service looks like this:
class Program
{
static void Main(string[] args)
{
Bus.Initialize(sbc =>
{
sbc.UseMsmq();
sbc.VerifyMsmqConfiguration();
sbc.UseMulticastSubscriptionClient();
sbc.ReceiveFrom("msmq://localhost/receiver");
});
ObjectFactory.Initialize(x =>
{
x.AddRegistry(new EmailSenderRegistry());
});
var host = HostFactory.New(c =>
{
c.SetServiceName("MyApp.EmailMessageReader");
c.SetDisplayName("MyApp.EmailMessageReader");
c.SetDescription("MyApp.EmailMessageReader");
c.Service<ClientService>(a =>
{
a.ConstructUsing(service => new ClientService());
a.WhenStarted(o => o.Start());
a.WhenStopped(x=>x.Stop());
});
});
host.Run();
}
}
internal class ClientService
{
public void Start()
{
Bus.Instance.SubscribeConsumer<EmailPrepService>();
}
public void Stop()
{
// do nothing
}
}
public class EmailPrepService : Consumes<EmailMessage>.All
{
public void Consume(EmailMessage message)
{
PrepareEmailForSending(message);
var emailService = ObjectFactory.GetInstance<IEmailSender>();
emailService.SendEmail(message);
}
}
My web application looks like this:
public class BusRegistry : Registry
{
public BusRegistry()
{
For<IServiceBus>().Singleton().Use(context => ServiceBusFactory.New(sbc =>
{
sbc.ReceiveFrom("msmq://localhost/sender");
sbc.UseMsmq();
sbc.UseMulticastSubscriptionClient();
sbc.UseControlBus();
sbc.Subscribe(subs => { subs.LoadFrom(context.GetInstance<IContainer>()); });
}));
}
}
Any help would be appreciated!
As you're using the MulticastSubscriptionClient, your subscriptions are 'transient' and not 'permanent', i.e. they are automatically unsubscribed at bus shutdown.
Your options are to a) use RabbitMQ as your transport or b) if you need to use MSMQ then use MassTransit's SubscriptionServices for subscriptions as they are then stored in a database and persist between bus instances.
See http://docs.masstransit-project.com/en/latest/overview/subscriptions.html for more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With