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.DependencyInjection
nuget restore
Once you have opened the Shuttle.DependencyInjection.sln
solution in Visual Studio set the following projects as startup projects:
Set
Shuttle.Core.Host.exe
as the Start external program option by navigating to the bin\debug folder of the server project for the Shuttle.DependencyInjection.Server project.
By default Shuttle.Esb does not require a dependency injection container. Shuttle makes use of an IMessageHandlerFactory
implementation to create message handlers. If no dependency injection container is required one could stick with the DefaultMessageHandlerFactory
instantiated by default.
The DefaultMessageHandlerFactory
requires message handlers that have a default (parameterless) constructor; else the instantiation of the handler will fail. In this guide we will use the WindsorContainer
that is part of the Castle Project.
In this guide we’ll create the following projects:
Shuttle.DependencyInjection.Client
Shuttle.DependencyInjection.Server
Shuttle.DependencyInjection.EMail
that will contain a fake e-mail service implementationShuttle.DependencyInjection.Messages
that will contain all our message classesCreate a new class library called
Shuttle.DependencyInjection.Messages
with a solution calledShuttle.DependencyInjection
Note: remember to change the Solution name.
Rename the
Class1
default file toRegisterMemberCommand
and add aUserName
property.
namespace Shuttle.DependencyInjection.Messages
{
public class RegisterMemberCommand
{
public string UserName { get; set; }
}
}
Add a new
Console Application
to the solution calledShuttle.DependencyInjection.Client
.
Install the
Shuttle.Esb.Msmq
nuget package.
This will provide access to the Msmq IQueue
implementation and also include the required dependencies.
Add a reference to the
Shuttle.DependencyInjection.Messages
project.
Implement the main client code as follows:
using System;
using Shuttle.Esb;
using Shuttle.DependencyInjection.Messages;
namespace Shuttle.DependencyInjection.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
});
}
}
}
}
}
The message sent will have its IgnoreTilleDate
set to 5 seconds into the future. You can have a look at the TransportMessage for more information on the message structure.
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.DependencyInjection.Messages" />
</messageRoute>
</messageRoutes>
</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.DependencyInjection.Messages
should be sent to endpoint msmq://./shuttle-server-work
.
To demonstrate the dependency injection we will create a fake e-mail service that we intend using in the server endpoint.
Add a new
Class Library
to the solution calledShuttle.DependencyInjection.EMail
.
Add an interface called
IEMailService
and implement it as follows:
namespace Shuttle.DependencyInjection.EMail
{
public interface IEMailService
{
void Send(string name);
}
}
Rename the default
Class1
file toEMailService
and implement theIEMailService
interfaces as follows:
using System;
using System.Threading;
namespace Shuttle.DependencyInjection.EMail
{
public class EMailService : IEMailService
{
public void Send(string name)
{
Console.WriteLine();
Console.WriteLine("[SENDING E-MAIL] : name = '{0}'", name);
Console.WriteLine();
Thread.Sleep(3000); // simulate communication wait time
Console.WriteLine();
Console.WriteLine("[E-MAIL SENT] : name = '{0}'", name);
Console.WriteLine();
}
}
}
Add a new
Class Library
to the solution calledShuttle.DependencyInjection.Server
.
Install both the
Shuttle.Esb.Msmq
andShuttle.Esb.Castle
nuget packages.
This will provide access to the Msmq IQueue
implementation and also include the required dependencies.
It will also include the WindsorContainer
implementation of the IMessageHandlerFactory
.
Install the
Shuttle.Core.Host
nuget 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 references to both the
Shuttle.DependencyInjection.Messages
andShuttle.DependencyInjection.EMail
projects.
Rename the default
Class1
file toHost
and implement theIHost
andIDisposabe
interfaces as follows:
using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Shuttle.Core.Host;
using Shuttle.DependencyInjection.EMail;
using Shuttle.Esb.Castle;
using Shuttle.Esb;
namespace Shuttle.DependencyInjection.Server
{
public class Host : IHost, IDisposable
{
private IServiceBus _bus;
private WindsorContainer _container;
public void Start()
{
_container = new WindsorContainer();
_container.Register(Component.For<IEMailService>().ImplementedBy<EMailService>());
// register all the message handler in this assembly
_container.Register(
Classes.FromThisAssembly()
.BasedOn(typeof(IMessageHandler<>))
.WithServiceFromInterface(typeof(IMessageHandler<>))
.LifestyleTransient()
);
_bus = ServiceBus.Create(
c => c.MessageHandlerFactory(new CastleMessageHandlerFactory(_container))
).Start();
}
public void Dispose()
{
_bus.Dispose();
}
}
}
Add an
Application Configuration File
item to create theApp.config
and 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
RegisterMemberHandler
that implements theIMessageHandler<RegisterMemberCommand>
interface as follows:
using System;
using Shuttle.Core.Infrastructure;
using Shuttle.DependencyInjection.EMail;
using Shuttle.Esb;
using Shuttle.DependencyInjection.Messages;
namespace Shuttle.DependencyInjection.Server
{
public class RegisterMemberHandler : IMessageHandler<RegisterMemberCommand>
{
private readonly IEMailService _emailService;
public RegisterMemberHandler(IEMailService emailService)
{
Guard.AgainstNull(emailService, "emailService");
_emailService = emailService;
}
public void ProcessMessage(IHandlerContext<RegisterMemberCommand> context)
{
Console.WriteLine();
Console.WriteLine("[MEMBER REGISTERED] : user name = '{0}'", context.Message.UserName);
Console.WriteLine();
_emailService.Send(context.Message.UserName);
}
}
}
This will write out some information to the console window. The injected e-mail service will also be invoked and you’ll see the result in the console window.
Set
Shuttle.Core.Host.exe
as 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 implemented dependency injection for message handlers.