Want to quickly learn what NServiceBus is all about? You're in the right place. In less than one hour, learn how to:
- Connect different parts of a system using messages
- Build a failure-resistant system using messages
- Create a system that can be easily extended as new requirements are added
If you've already experienced the benefits of messaging, check out our NServiceBus step-by-step tutorial.
- If you're new here, check out the overview of NServiceBus.
- The Glossary of messaging terms may help as you go along this tutorial.
- If you're already familiar with the basic benefits of messaging, check out our NServiceBus step-by-step tutorial for a deeper dive instead.
About the RetailDemo solution
The solution mimics a real-life retail system where a command to place an order is sent as a result of customer interaction. An event is published to kick off processes in the background. Using the publish-subscribe pattern allows us to isolate the component that performs billing from the one that places orders. This reduces coupling and makes the system easier to maintain in the long run. Later in this tutorial, you will learn how to add a second subscriber to that event in a new Shipping endpoint which will begin the process of shipping orders.
Download the RetailDemo solution
The solution has no prerequisites — no message queue or database to install, just a compatible IDE. To get started, download the solution, extract the files, and then open the RetailDemo.sln file.
RetailDemo Project structure
The solution contains five projects: Billing, ClientUI, Messages, Platform, and Sales.
The Billing, ClientUI, and Sales projects are endpoints. They communicate with each other using NServiceBus messages. The ClientUI endpoint is implemented as a web application and is the entry point to our system. The Sales and Billing endpoints, are console applications, that contain business logic related to processing and fulfilling orders.
Each endpoint project references the Messages assembly, which contains the shared definitions of messages as class files. The Platform project will provide a demonstration of the Particular Service Platform, but initially, its code is commented out.
Initial workflow
The ClientUI endpoint sends a PlaceOrder command to the Sales endpoint. As a result, the Sales endpoint will publish an OrderPlaced event using the publish/subscribe pattern, which will be received by the Billing endpoint, as shown in the diagram below.
Running the solution
The solution is configured to have multiple startup projects, so when you run the solution (Debug > Start Debugging or press F5) it should open three console applications, one for each messaging endpoint. Additionally, one of these will open the web application in your browser. The Particular Service Platform Launcher console app will also open but not do anything. Depending on your IDE, it may persist or immediately close.
Did all three windows appear?
- For Visual Studio Code users, ensure the Debug All launch configuration is selected from the dropdown list under the Run and Debug tab.
- In versions prior to Visual Studio 2019 16.1, there is a bug (Link 1, Link 2) that will sometimes prevent one or more projects from launching with an error message. If this is the case, stop debugging and try again. The problem usually happens only on the first attempt.
- For Rider users, follow the steps described on their documentation
In the ClientUI web application, click the Place order button to place an order, and watch what happens on the other windows.
It may happen too quickly to see, but the PlaceOrder command will be sent to the Sales endpoint. In the Sales endpoint window you will see:
INFO Received PlaceOrder, OrderId = 9b16a5ce
INFO Publishing OrderPlaced, OrderId = 9b16a5ce
As shown, when the Sales endpoint receives an PlaceOrder command, it publishes an OrderPlaced event, which will be received by the Billing endpoint. In the Billing endpoint window you will see:
INFO Billing has received OrderPlaced, OrderId = 9b16a5ce
In the ClientUI web application, go back and send more messages, watching the messages flow between endpoints.
Up next
Now that the project is up and running, let's break it!