When using Visual Studio 2015+ the NuGet packages should be restored automatically. If you find that they do not or if you are using an older version of Visual Studio please execute the following in a Visual Studio command prompt:
cd {extraction-folder}\Shuttle.Esb.Samples\Shuttle.RequestResponse
nuget restore
Once you have opened the Shuttle.RequestResponse.sln solution in Visual Studio set the following projects as startup projects:
Set
Shuttle.Core.Host.exeas the Start external program option by navigating to the bin\debug folder of the server project for the Shuttle.RequestResponse.Server project.
In order to get any processing done in Shuttle.Esb a message will need to be generated and sent to an endpoint for processing. The idea behind a command message is that there is exactly one endpoint handling the message. Since it is an instruction the message absolutely has to be handled and we also need to have only a single endpoint process the message to ensure a consistent result.
In this guide we’ll create the following projects:
Shuttle.RequestResponse.ClientShuttle.RequestResponse.ServerShuttle.RequestResponse.Messages that will contain all our message classesCreate a new class library called
Shuttle.RequestResponse.Messageswith a solution calledShuttle.RequestResponse
Note: remember to change the Solution name.
Rename the
Class1default file toRegisterMemberCommandand add aUserNameproperty.
namespace Shuttle.RequestResponse.Messages
{
public class RegisterMemberCommand
{
public string UserName { get; set; }
}
}
Add a new class called
MemberRegisteredEventalso with aUserNameproperty.
namespace Shuttle.RequestResponse.Messages
{
public class MemberRegisteredEvent
{
public string UserName { get; set; }
}
}
Add a new
Console Applicationto the solution calledShuttle.RequestResponse.Client.
Install the
Shuttle.Esb.Msmqnuget package.
This will provide access to the Msmq IQueue implementation and also include the required dependencies.
Add a reference to the
Shuttle.RequestResponse.Messagesproject.
Implement the main client code as follows:
using System;
using Shuttle.Esb;
using Shuttle.RequestResponse.Messages;
namespace Shuttle.RequestResponse.Client
{
class Program
{
static void Main(string[] args)
{
using (var bus = ServiceBus.Create().Start())
{
string userName;
while (!string.IsNullOrEmpty(userName = Console.ReadLine()))
{
bus.Send(new RegisterMemberCommand
{
UserName = userName
});
}
}
}
}
}
Create the shuttle configuration as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name='serviceBus' type="Shuttle.Esb.ServiceBusSection, Shuttle.Esb"/>
</configSections>
<serviceBus>
<messageRoutes>
<messageRoute uri="msmq://./shuttle-server-work">
<add specification="StartsWith" value="Shuttle.RequestResponse.Messages" />
</messageRoute>
</messageRoutes>
<inbox
workQueueUri="msmq://./shuttle-client-work"
errorQueueUri="msmq://./shuttle-error" />
</serviceBus>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
This tells shuttle that all messages that are sent and have a type name starting with Shuttle.RequestResponse.Messages should be sent to endpoint msmq://./shuttle-server-work.
Create a new class called
MemberRegisteredHandlerthat implements theIMessageHandler<MemberRegisteredEvent>interface as follows:
using System;
using Shuttle.Esb;
using Shuttle.RequestResponse.Messages;
namespace Shuttle.RequestResponse.Client
{
public class MemberRegisteredHandler : IMessageHandler<MemberRegisteredEvent>
{
public void ProcessMessage(IHandlerContext<MemberRegisteredEvent> context)
{
Console.WriteLine();
Console.WriteLine("[RESPONSE RECEIVED] : user name = '{0}'", context.Message.UserName);
Console.WriteLine();
}
}
}
Add a new
Class Libraryto the solution calledShuttle.RequestResponse.Server.
Install the
Shuttle.Esb.Msmqnuget package.
This will provide access to the Msmq IQueue implementation and also include the required dependencies.
Install the
Shuttle.Core.Hostnuget package.
The default mechanism used to host an endpoint is by using a Windows service. However, by using the Shuttle.Core.Host executable we are able to run the endpoint as a console application or register it as a Windows service for deployment.
Add a reference to the
Shuttle.RequestResponse.Messagesproject.
Rename the default
Class1file toHostand implement theIHostandIDisposabeinterfaces as follows:
using System;
using Shuttle.Core.Host;
using Shuttle.Esb;
namespace Shuttle.RequestResponse.Server
{
public class Host : IHost, IDisposable
{
private IServiceBus _bus;
public void Start()
{
_bus = ServiceBus.Create().Start();
}
public void Dispose()
{
_bus.Dispose();
}
}
}
Add an
Application Configuration Fileitem to create theApp.configand populate as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name='serviceBus' type="Shuttle.Esb.ServiceBusSection, Shuttle.Esb"/>
</configSections>
<serviceBus>
<inbox
workQueueUri="msmq://./shuttle-server-work"
errorQueueUri="msmq://./shuttle-error" />
</serviceBus>
</configuration>
Add a new class called
RegisterMemberHandlerthat implements theIMessageHandler<RegisterMemberCommand>interface as follows:
using System;
using Shuttle.Esb;
using Shuttle.RequestResponse.Messages;
namespace Shuttle.RequestResponse.Server
{
public class RegisterMemberHandler : IMessageHandler<RegisterMemberCommand>
{
public void ProcessMessage(IHandlerContext<RegisterMemberCommand> context)
{
Console.WriteLine();
Console.WriteLine("[MEMBER REGISTERED] : user name = '{0}'", context.Message.UserName);
Console.WriteLine();
context.Send(new MemberRegisteredEvent
{
UserName = context.Message.UserName
}, c => c.Reply());
}
}
}
This will write out some information to the console window and send a response back to the sender (client).
Set
Shuttle.Core.Host.exeas the Start external program option by navigating to the bin\debug folder of the server project.
Set both the client and server projects as the startup.
Execute the application.
The client application will wait for you to input a user name. For this example enter my user name and press enter:
You have now completed a full request / response call chain.