NServiceBus official documentation translation (2) NServiceBus introduction, nservicebus official
In this tutorial, we will learn how to create a very simple order system for sending messages from a client to the server. The system consists of three projects: Client, Server, and Messages. We will follow these steps to complete this task.
The complete solution code can be downloaded here.
Create a Client project
Let's start creating a Client project, which will be responsible for sending order requests to an NServiceBus Endpoint ).
Run Visual Studio as an administrator, add a class library project named "Ordering. Client", and name the solution "Ordering ".
Now we need to reference the NServiceBus assembly. The fastest way is to use the NuGet package management console.
Open the NuGet package management console:
Enter the following command:
PM> Install-Package NServiceBus.Host
Note: You need to reload the project
The package installer references the NServiceBus assembly and creates several template files in the Client project.
For example, "EndpointConfig. cs" is used to configure the end point of a configuration item, and the server configuration is automatically applied.
To change the configuration to client configuration, open the "EndpointConfig. cs" file and modify the following code:
namespace Ordering.Client{ public class EndpointConfig : IConfigureThisEndpoint, AsA_Server { }}
The modified code is as follows:
namespace Ordering.Client{ public class EndpointConfig : IConfigureThisEndpoint, AsA_Client { }}
Note: Only the inherited base classes are modified from AsA_Server to AsA_Client.
Later, we will add more code to the Client project. Now let's focus on how to process order requests first.
Create a Messages Project
First, add a class library project named "Ordering. Messages.
This project will contain message definitions and will be shared by the client and server, so that both sides will obtain a strong message description.
Install the "NServiceBusInterfaces" NuGet package in this project. On the package management console, enter the following command:
PM> Install-Package NServiceBus.Interfaces -ProjectName Ordering.Messages
Add a "PlaceOrder. cs" file and add the following code:
namespace Ordering.Messages{ public class PlaceOrder : ICommand { public Guid Id { get; set; } public string Product { get; set; } }}
Create a Server project
Now we can start to create a Server for processing orders. First, add a class library project named "Ordering. Server.
Install the "NServiceBusHost" NuGet package for this project. On the package management console, enter the following command:
PM> Install-Package NServiceBus.Host -ProjectName Ordering.Server
Note: You need to reload the project
Add references to the Messages project so that the server can interpret the message content.
Add a "PlaceOrderHandler. cs" file and write the following code to it:
namespace Ordering.Server{ public class PlaceOrderHandler : IHandleMessages<PlaceOrder> { public IBus Bus { get; set; } public void Handle(PlaceOrder message) { Console.WriteLine(@"Order for Product:{0} placed with id: {1}", message.Product, message.Id); } }}
Send order
We have almost finished it. Now the only thing left is to return to the Client project, add a reference to the Messages project, add a "SendOrder. cs" file, and write the following code to it:
namespace Ordering.Client{ public class SendOrder : IWantToRunWhenBusStartsAndStops { public IBus Bus { get; set; } public void Start() { Console.WriteLine("Press 'Enter' to send a message.To exit, Ctrl + C"); while (Console.ReadLine() != null) { var id = Guid.NewGuid(); Bus.Send("Ordering.Server", new PlaceOrder() { Product = "New shoes", Id = id}); Console.WriteLine("=========================================================================="); Console.WriteLine("Send a new PlaceOrder message with id: {0}", id.ToString("N")); } } public void Stop() { } }}
Note: The above code is based on version 4.x. If version 3.x is used, the "IWantToRunAtStartup" interface must be used.
Running Solution
The Code has been completed. Now it is time to run the solution.
Set the startup project in solution, and set the Client and Server project to run simultaneously.
Click F5 to run the solution.
Two console applications will be started. Press enter on the client console and you will see "Order for Product: New shoes placed" in the server window ".
Congratulations-you just completed your first NServiceBus program. Simple.
If you see warnings in the console, don't worry. These warnings are only NServiceBus telling you that it cannot find the desired queue and it will be automatically created for you.
What to learn next
- Fault tolerance handling for NServiceBus
- NServiceBus and SOA Architecture Principles
Coming soon.