Jason Rowe

Be curious! Choose your own adventure.

NServicebus and .NET Core

The following is a walk through of NServicebus and .NET Core. I setup a sample server and client using file system transport. In a future post, I’ll look at using RabbitMQ transport. The example code for this post can be found here GitHub JasonRowe/nservicebus_netcore.

Server, client, and shared project solution setup.

dotnet new console -n server
dotnet new console -n client
dotnet new classlib -n shared
dotnet new sln -n example
dotnet sln example.sln add **/*.csproj

After running all these commands you should have three projects all added to an example.sln. The next steps will add in all the classes you see in the image.

Installing the NServiceBus nuget packages.

I’ll use Visual Studio package manager console (Tools > Library Package Manager > Package Manager Console).

Get-Project server | Install-Package NServiceBus -Version 7.0.1
Get-Project server | Install-Package NServiceBus.Newtonsoft.Json -Version 2.1.0
Get-Project client | Install-Package NServiceBus -Version 7.0.1
Get-Project client | Install-Package NServiceBus.Newtonsoft.Json -Version 2.1.0

HelloWorld .NET Core NServiceBus.

For the hello world I’ll use training mode transport. In this mode, file storage is used for the transport. The following is my NServiceBus configuration for the server project’s Project.cs. I’ve also added a reference to the shared project created above as I will try to use conventions to define my commands instead of ICommand marker interfaces.

using System;
using NServiceBus;
using shared;

namespace server
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("example.server");

			var endpointConfiguration = new EndpointConfiguration("example.server");
			endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
			endpointConfiguration.EnableInstallers();
			endpointConfiguration.UsePersistence<LearningPersistence>();
			endpointConfiguration.UseTransport<LearningTransport>();
			endpointConfiguration.SendFailedMessagesTo("error");

			var conventions = endpointConfiguration.Conventions();
			conventions.DefiningCommandsAs(
				type =>
				{
					return type.Namespace == "shared";
				});

			var result = Endpoint.Start(endpointConfiguration).Result;
			Console.WriteLine("Press any key to exit");
			Console.ReadKey();
		}
	}
}

Also in the server project, I’ll create an example handler “ExampleMessageHandler.cs” and use a “ExampleMessage” class from the shared project which just has an ID property which is a Guid.

using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;
using shared;

namespace server
{
	public class ExampleMessageHandler : IHandleMessages<ExampleMessage>
	{
		static ILog log = LogManager.GetLogger<ExampleMessageHandler>();

		public Task Handle(ExampleMessage message, IMessageHandlerContext context)
		{
			log.Info($"Handling message id: {message.Id}");
			return Task.FromResult(0);
		}
	}
}

Now that we have a server setup waiting for messages it is time to work on the client. In the client project, I setup the Program.cs file to send example messages using the following code.

using System;
using NServiceBus;
using shared;

namespace client
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("example.client");

			var endpointConfiguration = new EndpointConfiguration(endpointName: "example.client");
			endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
			endpointConfiguration.EnableInstallers();
			endpointConfiguration.UsePersistence<LearningPersistence>();
			endpointConfiguration.UseTransport<LearningTransport>();
			endpointConfiguration.SendFailedMessagesTo("error");

			var conventions = endpointConfiguration.Conventions();
			conventions.DefiningCommandsAs(
				type =>
				{
					return type.Namespace == "shared";
				});

			var endpointInstance = Endpoint.Start(endpointConfiguration).Result;
			SendOrder(endpointInstance);

		}
		static void SendOrder(IEndpointInstance endpointInstance)
		{
			Console.WriteLine("Press enter to send a message");
			Console.WriteLine("Press any key to exit");

			while (true)
			{
				var key = Console.ReadKey();
				Console.WriteLine();

				if (key.Key != ConsoleKey.Enter)
				{
					return;
				}

				var exampleMessage = new ExampleMessage
				{
					Id = Guid.NewGuid()
				};

				endpointInstance.Send("example.server", exampleMessage);

				Console.WriteLine($"Sent a message with id: {exampleMessage.Id:N}");
			}
		}

	}
}


Setup both the server and client to startup and try sending some messages. You should see something similar to following after using the enter key to send a few test messages from the client.

2018-01-30 17:11:19.525 INFO server.ExampleMessageHandler Handling message id: 1782e09c-07ae-4040-b673-9be9923f7313
2018-01-30 17:11:25.171 INFO server.ExampleMessageHandler Handling message id: 6ddb9657-0fe5-4d3e-a681-aa32cfcb0ddd
2018-01-30 17:11:26.628 INFO server.ExampleMessageHandler Handling message id: 3f25555f-1154-4e1d-b14b-f1dc5a5d9b24


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *