Book Image

Learning NServiceBus

By : David Boike
Book Image

Learning NServiceBus

By: David Boike

Overview of this book

<p>Web service-based systems are designed using Remote Procedure Call (RPC) techniques. This technique is effective; however, this method creates problems with scalability and reusability as fault tolerance is inherently hindered by the RPC principles. This book helps you to overcome this problem by introducing you to the principles of messaging and SOA to build scalable and reliable systems, that are easy to extend and maintain.</p> <p>"Learning NServiceBus" is your essential guide to becoming a messaging guru. The book details the process of building a software system based on the SOA principles. From sending a simple message to publishing events, this book covers everything you need to know to understand and implement an SOA-based message driven systems.</p> <p>"Learning NServiceBus" will teach you how to use publish/subscribe to create an Serivce Oriented Architecture. We will then look how to manage and extend that architecture. Through the course of the book, you will build reliable systems which will automatically compensate for failures. You will also learn to build complex yet testable business processes which will make batch processes seem outdated. By the end of the book, you will realize that SOA is much more than just web services.</p>
Table of Contents (15 chapters)

Creating a message handler


Now we will create a message handler within our service.

  1. Add a new class to the service called UserCreator.cs.

  2. Add three namespaces to the using block of the class file:

    using NServiceBus;
    using NServiceBus.Logging;
    using UserService.Messages.Commands;
  3. Mark the class as public.

  4. Implement IHandleMessages<CreateNewUserCmd>.

  5. Implement the interface using Visual Studio's tools. This will generate a Handle(CreateNewUserCmd message) stub method.

Normally we would want to create the user here with calls to a database, but we don't have time for that! We're on a mission, so let's just demonstrate what would be happening by logging a message.

It is worth mentioning that a new feature in NServiceBus 4.0 is the ability to use any logging framework you like, without being dependent upon that framework. NServiceBus can automatically hook up to log4net or NLog—just add a reference to either assembly, and NServiceBus will find it and use it. You can even roll your own logging implementation if you wish.

However, it is not required to pick a logging framework at all. NServiceBus internalizes log4net, which it will use (via the NServiceBus.Logging namespace) if you don't explicitly include a logging library. This is what we will be doing in our example.

Now let's finish our fake implementation for the handler:

  1. Above the Handle method, add an instance of a logger:

    private static readonly ILog log = LogManager.GetLogger(typeof(UserCreator));
  2. To handle the command, remove NotImplementedException and replace it with:

      log.InfoFormat("Creating user '{0}' with email '{1}'",
        message.Name,
        message.EmailAddress);

When you're done, your class should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UserService.Messages.Commands;
using NServiceBus;

namespace UserService
{
  public class UserCreator : IHandleMessages<CreateNewUserCmd>
  {
    private static readonly ILog log = 
      LogManager.GetLogger(typeof(UserCreator));

    public void Handle(CreateNewUserCmd message)
    {
      log.InfoFormat("Creating user '{0}' with email '{1}'",
        message.Name,
        message.EmailAddress);
    }
  }
}

Now we have a command message and a service endpoint to handle it. It's OK if you don't understand quite how it all connects quite yet. Next we need to create a way to send the command.