Skip to content

Quick start

Kévin LOVATO edited this page May 28, 2014 · 17 revisions

This page will walk you through the necessary steps to have a Bus working between two applications.

Creating the solution

First create two applications, along with their test projects, and add one Class Library per application to share Messages between apps (we use the convention <Application>.Messages but you can name it whatever you want).

Then add a reference to the Zebus package in your projects and a reference to Protobuf-net in your Messages projects.

quick_start_solution_overview

Creating the Messages

Just create the desired messages in the "Messages" assemblies, make them Protobuf serializable and inherit either IEvent or ICommand.

Here's one of the Commands of the example project:

[ProtoContract]
public class MakeTeaCommand : ICommand
{
    [ProtoMember(1)]
    public int OrderNumber { get; set; }
}

Creating the Handlers

Create a Handler for each of your Messages, and make them inherit IMessageHandler.

Here's one of the Handlers of the example project:

public class MakeTeaCommandHandler : IMessageHandler<MakeTeaCommand>
{
    private readonly IBus _bus;

    public MakeTeaCommandHandler(IBus bus)
    {
        _bus = bus;
    }

    public void Handle(MakeTeaCommand message)
    {
        Console.WriteLine("Making tea for order number: " + message.OrderNumber);
        _bus.Publish(new TeaCupIsReady { OrderNumber = message.OrderNumber });
    }
}

Bus setup

Just create a BusFactory and configure it, then start the Bus and you're ready to go.

Main method of my "Paris" app:

class Program
{
    static void Main()
    {
        var factory = new BusFactory().WithConfiguration("tcp://localhost:129", "Demo")
                                      .WithPeerId("Paris.0")
                                      .WithScan();

        using (var bus = factory.CreateAndStartBus())
        {
            Console.WriteLine("Press a key to send a MakeTeaCommand");
            Console.ReadKey();
            bus.Send(new MakeTeaCommand { OrderNumber = 123 });

            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
    }
}

Directory setup

You need a Directory to hold the state of your Bus, an easy way to achieve that is to install the Zebus.Directory.Standalone package and start-up the Directory (the instructions will be displayed after installing the package).

Conclusion

Now you just have to run your applications and they will register to the Directory and send Messages to each other.