However as I was trying to roll it out into production I realized that using the standard Spring configuration wasn't going to work for us. I wanted the name of the Msmq input queue to be specified programatilcy instead of via a config file. I knew it had to be possible but I had a really tough time figuring out how to do it. All the samples I could find were about how to set things up with Spring. So I though I would share how I did it.
The standard example shows:
NServiceBus.Configure.With().XmlSerializer()
.MsmqTransport()
.IsTransactional(false) .PurgeOnStartup(false).UnicastBus()
.ImpersonateSender(false).LoadMessageHandlers()
.CreateBus()
.Start();
So to dynamically set the MsmqTransoprt values you can write the following:
Configure configure = NServiceBus.Configure.With().SpringBuilder();
MsmqTransport transport = configure.Configurer.ConfigureComponent<MsmqTransport>(ComponentCallModelEnum.Singleton);
transport.InputQueue = "DynamicQueueName";transport.NumberOfWorkerThreads = 1;
transport.ErrorQueue = "DynamicErrorQueue";transport.MaxRetries = 5;
configure
.XmlSerializer()
.UnicastBus()
.ImpersonateSender(false).LoadMessageHandlers()
.CreateBus()
.Start();
This works for me, I have no idea if that is the best practice or if there is a better way to set these values programaticly. Hopefully this helps you and saves you from some the headache I went through (it always seems pretty simple once you get it figured out). Please let me know if you know a better way to do this.
2 comments:
That's the right way to go about doing it (although it will be slightly changing in the next version).
I believe this syntax has recently changed. ConfigureComponent will not cast to MsmqTransport.
I believe the current syntax is
Configure configure = NServiceBus.Configure.With().DefaultBuilder();
var transport = configure.Configurer.ConfigureComponent(ComponentCallModelEnum.Singleton);
transport.ConfigureProperty(t => t.InputQueue, "DynamicQueue");
transport.ConfigureProperty(t => t.ErrorQueue, "DynamicQueue_Error");
transport.ConfigureProperty(t => t.MaxRetries, 5);
transport.ConfigureProperty(t => t.NumberOfWorkerThreads, 1);
transport.ConfigureProperty(t => t.IsTransactional, true);
transport.ConfigureProperty(t => t.PurgeOnStartup, true);
var Bus = configure
.BinarySerializer()
.RijndaelEncryptionService()
.UnicastBus()
.ImpersonateSender(false)
.DoNotAutoSubscribe()
.LoadMessageHandlers()
.CreateBus()
.Start();
Post a Comment