Book Image

Learning NServiceBus - Second Edition

By : David Boike
Book Image

Learning NServiceBus - Second Edition

By: David Boike

Overview of this book

Table of Contents (18 chapters)
Learning NServiceBus Second Edition
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a message assembly


The first step while creating an NServiceBus system is to create a messages assembly. Messages in NServiceBus are simply plain old C# classes. Like the WSDL document of a web service, your message classes form a contract by which services communicate with each other.

Follow these steps to create your solution:

  1. In Visual Studio, create a new project by creating a new class library. Name the project UserService.Messages and the solution, simply Example. This first project will be your messages assembly.

  2. Delete the Class1.cs file that came with the class project.

  3. From the NuGet Package Manager Console, run this command to install the NServiceBus package, which will add the reference to NServiceBus.Core.dll:

    PM> Install-Package NServiceBus –ProjectName UserService.Messages
    
  4. Add a new folder to the project called Commands.

  5. Add a new class file called CreateNewUserCmd.cs to the Commands folder.

  6. Add using NServiceBus; to the using block of the class file. It is very helpful to do this first so that you can see all the options available with IntelliSense.

  7. Mark the class as public and implement ICommand. This is a marker interface, so there is nothing you need to implement.

  8. Add the public properties for EmailAddress and Name.

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 NServiceBus;

namespace UserService.Messages.Commands
{
  public class CreateNewUserCmd : ICommand
  {
    public string EmailAddress { get; set; }
    public string Name { get; set; }
  }
}

Congratulations! You've created a message. This new message will form the communication contract between the message sender and receiver. This is just a message; now you need to create a service that can receive and process it.