Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning NServiceBus - Second Edition
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning NServiceBus - Second Edition

Learning NServiceBus - Second Edition - Second Edition

By : David Boike
4.8 (6)
close
close
Learning NServiceBus - Second Edition

Learning NServiceBus - Second Edition

4.8 (6)
By: David Boike

Overview of this book

If you are a .NET developer who wants to eliminate the problems related to defective third-party web service integration or batch job failures, then this is the book for you. It is also perfect for those of you who are new to NServiceBus and service-oriented architecture and would like to learn how you can streamline all of your development efforts.
Table of Contents (12 chapters)
close
close
11
Index

Creating a message handler

Now that we have a service endpoint to host our code, we will create a message handler within the endpoint that will process our message when it arrives:

  1. Add a new class called UserCreator.cs to the service.
  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. In order to keep the examples straightforward, we will skip the details of database access and just demonstrate what will happen by logging a message.

With NServiceBus, you can use any logging framework you like without being dependent upon that framework. NServiceBus internally includes a logging system that logs to both console and file, with an API that looks very much like log4net (previous versions of NServiceBus actually used the log4net framework directly). In Chapter 7, Advanced Configuration, you will learn how to easily swap these out for the real log4net framework, NLog framework, or implement an adapter for any logger we like. For now, we are more than content with the built-in logging implementation via the NServiceBus.Logging namespace.

Now lets 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 the following statement:
      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 System.Threading.Tasks;
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. Its okay if you don't understand quite how all of this connects yet. Next, we need to create a way to send the command.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning NServiceBus - Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon