We have seen how to setup NServiceBus in order to use the built-in host environment and we have seen the basic concepts behind NServiceBus. Now that we have something up & running how can we use it?


Before we start diving in what we can do with a running server, let’s go back for a while and fire up the Computer Management snap-in and take a look at the “Private Queues” under “Services and Applications\Message Queuing”:


As you can see the NServiceBus server has created 2 queues for us: one with the same name as our C# project and the other one with the “.retries” postfix.

  • The SampleBusServer queue is the main queue, the role of this queue is to handle incoming messages for the server endpoint;
  • The SampleBusServer.Retries is automatically created by the NServiceBus infrastructure and is used to handle the second level retries;

A sample client

So far so good, but how can we use all this infrastructure? creating a “client” application:

  1. Add to the solution a new console application project called “SampleBusClient”;
  2. Add to the solution a new class library project called “SampleBus”;

The first thing we need to do is to configure the client application to use the bus:

  1. Add a reference via nuget to NServiceBus to the client application;
  2. Add the following code to the Program class:
static void Main( string[] args )
var _bus = NServiceBus.Configure.With()
.IsTransactional( true )
.PurgeOnStartup( false )
.Start( () =>
} );

this is the way we can use to create a bus instance manually in order to host the bus in our own process without relying on the NServiceBus.Host infrastructure. What are we doing?

  1. NServiceBus relies on a IoC container to work, using the DefaultBuilder we are instructing the bus to use its own internal implementation;
  2. Log4Net defines the the default logger will be log4net;
  3. we then disable the timeout manager since we are not interested to use it in this sample;
  4. we use the xml serializer and message queue as the transport;
    • configuring it as transactional;
    • disabling the queue purge at startup;
  5. we finally start the bas using the Unicast implementation, currently the only one on premise;
    • the ForInstallationOn<Windows> instructs the bus to create the queues if they do not exist; be aware that the process must have the necessary permissions;

if we run the sample nothing happens but if we take a look at the list of MSMQ we immediately see that 2 more queues have been created for us:


The communication

the final step is to allow communication between the two processes:

  1. Add a reference via nuget to NServiceBus to the SampleBus class library;
  2. Add to the SampleBus class library a new class called SampleMessage (that implements the empty IMessage interface):

    namespace SampleBus
    public class SampleMessage : IMessage
    public String Text { get; set; }

  3. Configure the SampleBusClient, using the app.config file, to send messages found in the SampleBus assembly to the server queue:


  4. Add a message handler to the SampleBusServer project:

    class SampleMessageHandler : IHandleMessages<SampleMessage>
    public void Handle( SampleBus.SampleMessage message )
    var color = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.Yellow;

    Console.WriteLine( message.Text );

    Console.ForegroundColor = color;

    we are instructing NServiceBus that when a message of type SampleMessage is posted to our queue (SampleBusServer) we want to handle it using the provided message handler;

Ready to rock

Return to the client application and add the last piece:

_bus.Send( new SampleBus.SampleMessage() { Text = "Hi there!" } );

we use the bus to send our first message, startup the client and let it run, it should start and close without any exception, now run the server:


Our handler is called by the infrastructure and the message is displayed. Lovely.

I’m pretty sure you all have noticed that the whole infrastructure is asynchronous, we are not running client and server at the same time, and durable, we can even restart the OS after the client is run and before running the server.

The full source code of the sample is available on my public SkyDrive.