Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nservicebus msmqtransport with code

Tags:

nservicebus

I'm just geting started with NServiceBus and can't figure out what I'm missing when configuring the MsmqTransport in code. If I configure the publisher like this;

IBus bus = Configure.With()
                        .CastleWindsorBuilder()
                        .XmlSerializer()
                        .MsmqSubscriptionStorage()
                        .MsmqTransport()
                            .IsTransactional(true)
                            .PurgeOnStartup(false)
                        .UnicastBus()
                            .ImpersonateSender(false)
                        .CreateBus()
                        .Start();
bus.Publish(new Message(DateTime.Now));

and the app.config like so


<configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
</configSections>
<MsmqTransportConfig 
    InputQueue="testapps_messagebus" 
    ErrorQueue="testapps_errors" 
    NumberOfWorkerThreads="1" 
    MaxRetries="5" />

Then all works fine - it will create the queues and I can happily message away, however if I delete the queues and then try again with code like so;

var config = Configure.With()
              .CastleWindsorBuilder()
              .XmlSerializer()
              .UnicastBus()
                  .ImpersonateSender(false)
              .MsmqSubscriptionStorage();
config
    .Configurer
    .ConfigureComponent<MsmqTransport>(NServiceBus.ObjectBuilder.ComponentCallModelEnum.None)
        .ConfigureProperty(x => x.InputQueue, "testapps_messagebus")
        .ConfigureProperty(x => x.NumberOfWorkerThreads, 1)
        .ConfigureProperty(x => x.ErrorQueue, "testapps_errors")
        .ConfigureProperty(x => x.MaxRetries, 5);

IBus bus = config
         .CreateBus()
         .Start();

bus.Publish(new Message(DateTime.Now));

The messages seem to get lost as they do not appear in any queues nor get handled - I'm guessing I'm missing something but I can't see where.

like image 272
Gareth Avatar asked Nov 18 '09 14:11

Gareth


1 Answers

D'Oh! Post a question that you've been puzzeling over for a while and take a break. Then of course the answer hits you and it's totaly obvious! I was forgetting to configure the MsmqTransport, my working code is below for anyone that's interested.


Configure config = Configure.With();
config
    .CastleWindsorBuilder()
    .XmlSerializer()
    .MsmqSubscriptionStorage()
    .MsmqTransport()
        .IsTransactional(true)
        .PurgeOnStartup(false)
    .UnicastBus()
        .ImpersonateSender(false);

config
    .MsmqSubscriptionStorage()
    .Configurer
        .ConfigureComponent(NServiceBus.ObjectBuilder.ComponentCallModelEnum.None)
            .ConfigureProperty(x => x.InputQueue, "testapps_messagebus")
            .ConfigureProperty(x => x.NumberOfWorkerThreads, 1)
            .ConfigureProperty(x => x.ErrorQueue, "testapps_errors")
            .ConfigureProperty(x => x.MaxRetries, 5);

IBus bus = config
              .CreateBus()
              .Start();

bus.Publish(new Message(DateTime.Now));

like image 52
Gareth Avatar answered Oct 09 '22 13:10

Gareth