0

I'm trying to implement a simple POC for a NServiceBus solution. So I implemented 3 endpoints, let's call them A,B and C. A is self hosted in a console app, B and C are NSB hosts. In A I'm sending a command DoFoo, in B I'm handling DoFoo and inside the handler I publish several events of type BarOccurred like so:

for (int i=0; i<5; i++)
{
  Bus.Publish(new BarOccurred);
  Thread.Sleep(1000);
}

In C I'm subscribing and handling it by writing to the console that Bar occurred.

I expected to see the message handled in C every time that B publishes it (i.e every second). However the behavior I see is that only after the command handler exit (after 5 iterations) only then all 5 messages are received by C.

Is that the expected behavior for the default configuration? Is there anything I need to declare in order to have messages sent and processed as soon as they are published?

moranlf
  • 554
  • 5
  • 19

1 Answers1

0

The Bus.Publish is in a single transaction and commits once all messages have been sent. I would see if packaging all the messages in one Bus.Publish(IMesssage[]) may meet your needs. This turns into 1 message on the wire.

Adam Fyles
  • 6,030
  • 1
  • 23
  • 29
  • I think it might be better to say that the message processing is (by default) a single transaction - including all publishes, meaning that nothing is published until the handler completes. – Udi Dahan Jan 10 '14 at 22:35
  • Thanks Adam and @UdiDahan. How would one change the default configuration to support my scenario? In my system, I have a command that is triggered from the UI and starts a process of scanning. I want that each time a frame is acquired, some image processing algorithm would process that frame. I want the processing and scanning to run concurrently. What is the configuration/implementation that I should use? – moranlf Jan 11 '14 at 18:34
  • 1
    In this case, it could be that disabling transactions would be the way to go. – Udi Dahan Jan 11 '14 at 21:28
  • @UdiDahan thanks again! Can one disable transactions for a specific message type? for a single endpoint? all commands? If someone would put it in an answer form with some code and explanations that'd be great. – moranlf Jan 12 '14 at 04:42
  • 1
    Disabling transactions can be done per endpoint only right now. – Udi Dahan Jan 12 '14 at 10:05
  • @UdiDahan I would accept your suggestion to disable transactions for the endpoint as an answer, but I can't vote for the current answer. – moranlf Jan 12 '14 at 11:23
  • 1
    `TransactionScope` is thread-safe so it may be an option to leave the endpoint transactional and implement a worker thread for the scanning / publishing. – Eben Roux Jan 13 '14 at 04:00