(For more resources related to this topic, see here.)
Before diving in, we should take a moment to consider why NServiceBus might be a tool worth adding to your repertoire. If you’re eager to get started, feel free to skip this section and come back later.
So what is NServiceBus? It’s a powerful, extensible framework that will help you to leverage the principles of Service-oriented architecture ( SOA ) to create distributed systems that are more reliable, more extensible, more scalable, and easier to update.
That’s all well and good, but if you’re just picking up this book for the first time, why should you care? What problems does it solve? How will it make your life better?
Ask yourself whether any of the following situations describe you:
If any of these situations has you nodding your head in agreement, I invite you to read on.
NServiceBus will help you to make multiple transactional updates utilizing the principle of eventual consistency so that you do not encounter deadlocks. It will ensure that valuable customer order data is not lost in the deep dark depths of a multi-megabyte log file.
By the end of the book, you’ll be able to build systems that can easily scale out, as well as up. You’ll be able to reliably perform non-transactional tasks such as calling web services and sending emails. You will be able to easily start up long-running processes in an application server layer, leaving your web application free to process incoming requests, and you’ll be able to unravel your spaghetti codebases into a logical system of commands, events, and handlers that will enable you to more easily add new features and version the existing ones.
You could try to do this all on your own by rolling your own messaging infrastructure and carefully applying the principles of service-oriented architecture, but that would be really time consuming. NServiceBus is the easiest solution to solve the aforementioned problems without having to expend too much effort to get it right, allowing you to put your focus on your business concerns, where it belongs.
So if you’re ready, let’s get started creating an NServiceBus solution.
We will be covering a lot of information very quickly in this article, so if you see something that doesn’t immediately make sense, don’t panic! Once we have the basic example in place, we will loop back and explain some of the finer points more completely.
There are two main ways to get the NServiceBus code integrated with your project, by downloading the Windows Installer package, and via NuGet. I recommend you use Windows Installer the first time to ensure that your machine is set up properly to run NServiceBus, and then use NuGet to actually include the assemblies in your project.
Windows Installer automates quite a bit of setup for you, all of which can be controlled through the advanced installation options:
Download the installer from http://particular.net/downloads and install it on your machine. After the install is complete, everything will be accessible from your Start Menu. Navigate to All Programs | Particular Software | NServiceBus as shown in the following screenshot:
The install package includes several samples that cover all the basics as well as several advanced features. The Video Store sample is a good starting point. Multiple versions of it are available for different message transports that are supported by NServiceBus. If you don’t know which one to use, take a look at VideoStore.Msmq.
I encourage you to work through all of the samples, but for now we are going to roll our own solution by pulling in the NServiceBus NuGet packages.
Once your computer has been prepared for the first time, the most direct way to include NServiceBus within an application is to use the NuGet packages.
There are four core NServiceBus NuGet packages:
The NuGet packages will also attempt to verify that your system is properly prepared through PowerShell cmdlets that ship as part of the package. However, if you are not running Visual Studio as an Administrator, this can be problematic as the tasks they perform sometimes require elevated privileges. For this reason it’s best to run Windows Installer before getting started.
The first step to 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.
For this example, let’s pretend we’re creating a website like many on the Internet, where users can join and become a member. We will construct our project so that the user is created in a backend service and not in the main code of the website.
Follow these steps to create your solution:
PM> Install-Package NServiceBus.Interfaces –ProjectName UserService.Messages
When you’re done, your class should look like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 will form the communication contract between the message sender and receiver. Unfortunately, we don’t have enough to run yet, so let’s keep moving.
Now we’re going to create a service endpoint that will handle our command message.
PM> Install-Package NServiceBus.Host –ProjectName UserService
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…