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:
In Visual Studio, create a new project by creating a new class library. Name the project
UserService.Messages
and the solution, simplyExample
. This first project will be your messages assembly.Delete the
Class1.cs
file that came with the class project.From the NuGet Package Manager Console, run this command to install the
NServiceBus
package, which will add the reference toNServiceBus.Core.dll
:PM> Install-Package NServiceBus –ProjectName UserService.Messages
Add a new folder to the project called
Commands
.Add a new class file called
CreateNewUserCmd.cs
to theCommands
folder.Add
using NServiceBus;
to theusing
block of the class file. It is very helpful to do this first so that you can see all the options available with IntelliSense.Mark the class as
public
and implementICommand
. This is a marker interface, so there is nothing you need to implement.Add the
public
properties forEmailAddress
andName
.
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.