开发者

How to create a publisher within a console application

I'm just starting out with NServiceBus and have updated the PubSub sample to work with .NET 4.0 Framework. That's working perfectly ok. This runs one publisher and two subscribers within the "NServiceBus.Host.exe" environment - so it is that which takes responsibility for setting up an instance of the Bus and doing any relevant subscriptions. That's all working fine (as you'd expect) but I'm now trying to move the publisher out of being run within "NServiceBus.Host.exe" into it's own console application (eventually I would like to publish messages from a website so this seems a good small step in that direction).

If I start the 3 projects (my console app, Sub1 and Sub2) it creates 5 msmq on my local machine, but instead of the endpoint.config...subscriptions Q, it creates a generic "nservicebus_subscriptions" Q. If I enable journals, I see that the MyPublisherInputQueue has three (processed) completion messages with an errorcode 0, whilst the subscriber1inputqueue and subscriber2inputqueue each have one. This all seems good, but if I then publish messages, the publisher doesn't appear to throw any errors, but no messages make it as far as the subscribers (they just sit waiting for a message). Also no messages appear in either the new message or journal for any of the MQs.

I'm obviously missing some step(s) - Is the console application not opening itself up for subscriptions? If so, what steps are needed to do that? What steps am I missing that run when you host the publisher in the nservicebus.host.exe?

To create the console application I've done the following:

  1. Within the pubsub solution, created a console application.
  2. Added references in the console application to mymessages, nservicebus.dll and nservicebus.cor开发者_Go百科e.dll
  3. From the existing pub code and example on the NServiceBus website, added following code to "Main()"

Code:

IBus Bus = Configure.With()
                    .Log4Net()
                    .DefaultBuilder()
                    .MsmqSubscriptionStorage()
                    .XmlSerializer()
                    .MsmqTransport()
                    .UnicastBus()
                        .LoadMessageHandlers()
                    .CreateBus()
                    .Start();

bool publishIEvent = true;
do
{
    Console.ReadLine();
    var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

    eventMessage.EventId = Guid.NewGuid();
    eventMessage.Time = DateTime.Now.Second > 30 ? (DateTime?)DateTime.Now : null;
    eventMessage.Duration = TimeSpan.FromSeconds(99999D);

    Bus.Send(eventMessage);

    Console.WriteLine("Published event with Id {0}.", eventMessage.EventId);

} while (true);
  1. Created an app.config for the new console application using the contents of the existing "publisher" app.config.

  2. Added "NServiceBus.Integration" to command line arguments for console application project.


If you look at your logs, it is likely that NServiceBus is telling you that you need to have your publisher be configured to be transactional. The way to do that is to include .IsTransactional(true) after .MsmqTransport().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜