Skip to content
Jens Kristian Villadsen edited this page Nov 28, 2013 · 19 revisions

This is a very basic example on how to get started.

Create a C# project - any kind of project really - and use NuGet to pull down the Rebus package, and then find someplace where you can write the following magic spells (in this case, we're pretending to be in a console application):

// we have the container in a variable, but you would probably stash it in a static field somewhere
using(var adapter = new BuiltinContainerAdapter())
{
    Configure.With(adapter)
        .Transport(t => t.UseMsmqAndGetInputQueueNameFromAppConfig())
        .MessageOwnership(d => d.FromRebusConfigurationSection())
        .CreateBus().Start();

    Console.WriteLine("Press enter to quit");
    Console.ReadLine();
} //< always dispose bus when your app quits - here done via the container adapter

and then, before you run it, add the following XML abracadabra to your app.config/web.config:

<configSections>
    <section name="rebus" type="Rebus.Configuration.RebusConfigurationSection, Rebus" />
</configSections>

<rebus inputQueue="my-app.input" errorQueue="my-app.error" workers="1">
    <endpoints>
        <!-- <add messages="SomeAssembly" endpoint="another-app.input"/> -->
    </endpoints>
</rebus>

Then, see if you can run the program without getting slapped in the face with exceptions and whatnot. If so, try adding a handler to the simple builtin container adapter:

adapter.Register(typeof(PrintDateTime));

and the handler looks like this:

public class PrintDateTime : IHandleMessages<DateTime>
{
    public void Handle(DateTime currentDateTime)
    {
        Console.WriteLine("The time is {0}", currentDateTime);
    }
}

and then make your app send the current time to itself by doing something like this (before the Console.ReadLine(), obviously ;)):

var timer = new System.Timers.Timer();
timer.Elapsed += delegate { adapter.Bus.SendLocal(DateTime.Now); };
timer.Interval = 1000;
timer.Start();

which will make the app send the current time to itself every second.

The code shown here can be found in a fully functional and runnable version (using the Windsor container adapter) here: https://github.com/rebus-org/RebusSamples/tree/master/TimePrinter

Clone this wiki locally