Now that Rebus is present on NuGet, it has become extremely easy to get started. This small guide will show how to build a simple TopShelf service, that will send a message to itself. It should be pretty easy to extend this to a more complete example with multiple services, but let’s just keep it really simple for now.
Create the host process
First, we create a TopShelf host process that has all the necessary stuff to be able to run and be debugged, as well as installed, uninstalled, and be managed as a Windows Service. Precious stuff when you’re building backend processes.
Create a Console Application
Do what the header says – and change the target framework from that bastard “.NET 4 Framework Client Profile” to “.NET 4 Framework”.
Go get TopShelf
Open a package manager console (we’ll need it for the Rebus alpha packages), and
1 2 |
Install-Package TopShelf -Project <NameOfYourProject> Install-Package Log4Net -Project <NameOfYourProject> |
For some reason, you need to download Log4Net manually, even though TopShelf depends on it.
Configure Log4Net and declare the TopShelf service
Now, do something like this in your Program.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using Topshelf; using log4net.Config; class Program { static void Main() { XmlConfigurator.Configure(); HostFactory .Run(c => { c.SetServiceName("RebusTest"); c.SetDisplayName("RebusTest"); c.Service<Program>(p => { p.ConstructUsing(() => new Program()); p.WhenStarted(s => s.Start()); p.WhenStopped(s => s.Stop()); }); }); } void Start() { } void Stop() { } } |
and then add some XML to your app.config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="White" /> <backColor value="Red" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <priority value="DEBUG" /> <appender-ref ref="ColoredConsoleAppender" /> </root> </log4net> |
Now you should be able to run the service and see a buttload of log statements, courtesy of TopShelf.
Insert Rebus
Now that we have the host process up and running, let’s make the program do stuff.
Let’s get Rebus
Go to the package manager console again, and do this:
1 2 |
Install-Package Rebus.Log4Net -Pre -Project <NameOfYourProject> Install-Package Rebus.Castle.Windsor -Pre -Project <NameOfYourProject> |
Let’s configure and use it!
Now, you should be able to implement your Start and Stop methods like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
IBus bus; IWindsorContainer container; void Start() { container = new WindsorContainer(); container.Register(Component.For<IHandleMessages<string>>().Instance(this)); bus = Configure.With(new WindsorContainerAdapter(container)) .Logging(l => l.Log4Net()) .Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig()) .Serialization(s => s.UseJsonSerializer()) .DetermineEndpoints(d => d.FromRebusConfigurationSection()) .CreateBus() .Start(); bus.Send("HELLLLLO WORLD!!!1"); } void Stop() { container.Dispose(); } |
Now, in order to make the container.Register(...) line from above compile, you need to let Program implement IHandleMessages<string>. Try adding something like this:
1 2 3 4 5 6 7 8 9 |
class Program : IHandleMessages<string> { // (... cut for brevity ....) public void Handle(string message) { Console.WriteLine("w00t!!!: {0}", message); } } |
If you run the program now, you’ll get some kind of error, saying that the input queue name is invalid. That’s because we haven’t configured Rebus yet. Let’s do that by modifying app.config by adding this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- (...) --> <section name="rebus" type="Rebus.Configuration.RebusConfigurationSection, Rebus" /> </configSections> <!-- (...) --> <rebus inputQueue="RebusTest.input" workers="2"> <endpoints> <add messages="System.String, mscorlib" endpoint="RebusTest.input" /> </endpoints> </rebus> </configuration> |
The InputQueue attribute above tells Rebus to receive messages from the RebusTest.input queue, and the <add ...> elements says to send messages of the System.String type to the same RebusTest.input queue.
Let’s run it
Now you should be able to run it and see something like this on your screen:
If you get the same w00t!!!: HELLLLLO WORLD!!!1 in your console, it means you’re ready to start building stuff with Rebus 🙂