Hendrik Bulens All about .NET development

Getting started with NServiceBus

In this blog post we’ll take a look at NServiceBus, a popular open-source service for .NET. With an easy to follow tutorial, you’ll have a working service bus up and running within minutes.

For the VS2015 solution, download it here.

In order to make this work, we’ll need to do some boring stuff first, starting with setting up the Visual Studio Solution. Two console applications and one class library need to be created. One console application serves as the publisher, the other will serve as the subscriber. The class library will contain shared classes for both publisher and subscriber. In other words, add a reference to the **shared **library for the two console applications. Note the words marked in bold: these terms have their purpose:

  • The publisher serves as the initiating component for the service bus. An example of a publisher could be a callback method from the UI (clicks, select, …), but it might as well be scheduled job that sends out alert messages to a mail service.
  • The subscriber serves as the component that processes incoming messages. For instance, when the service bus sends out a message of a specific type (e.g. EmailMessage), any component that has registered to this kind of message will receive it – and can do with it whatever it wants. This is the place where you will usually add all of your core functionality (like sending out e-mails, saving items to the database, logging, …).
  • The shared component makes sure the subscriber and publisher can understand each other by defining contracts for both. This is not more than some simple view model classes that inherit from certain interfaces.

Right, enough with the theory, let’s get back to business.

For each project, install the NServiceBus Nuget Package:
PM> Install-Package NServiceBus -Version 5.2.3

If you didn’t register to their services yet, a popup window might redirect you to their site to register for a trial. Once that’s settled, we can start the actual programming. First thing we need to do is create a data contract for the code that will publish a message to the service bus. To do this, create a simple class that inherits the IEvent interface in the NServiceBus namespace. To see an example, see code sample below. You’ll also notice a ConfigErrorQueue: this is needed for configuring the service bus later on.

For this basic tutorial, we’re ready to setup the subscriber and the publisher. The first project – the publisher – is where you’ll initiate the process. By hitting the ENTER key in the console window, the publishing project will send a message to the service bus. The service bus will process this message and will send it to the components that have registered to accept this kind of messages. The full code of the publisher:

Note that the default transport is MSMQ, which doesn’t require additional configuration. Three other options are possible:

  • Azure: could be either servicebus or storage queues
  • Sql Server
  • RabbitMQ

If you want to work with Azure, add this line of code to your configuration:

You don’t need to do extra work to get this transport running, all you need is a correct connection string to the service bus. NServiceBus will take care of the queue provisioning. If you want to work with SQL Server, add this line of code to your configuration:

You don’t need to do extra work to get this transport running, all you need is a correct connection string. NServiceBus will take care of the table provisioning. The full code for the subscribing part:

In this project – the subscriber – we’ll also need to add some configuration in the app.config. You can copy/paste this configuration, you only need to modify the assembly name of your shared project (where the config class is located) and the name of the publisher’s endpoint that you’ve hardcoded in the console application’s main method.

You’re almost all set. Modify the VS Solution to start multiple projects. Select the two console applications and fire up the solution. If all went well, you’ll now see two console windows. Look for the publishing window and hit enter: notice how a new line appears in the other console window? You can keep on doing this until you’re bored, the publishing/subscription system work independently yet related due to the service bus.

Wasn’t that easy? I expected it to be much more difficult but it’s quite simple to work with.

Add comment

Leave a Reply

By Hendrik Bulens
Hendrik Bulens All about .NET development