Sending from a Nancy module

Component: NServiceBus
NuGet Package NServiceBus (5.x)

Initialize the Nancy web endpoint

Open Bootstraper.cs and look at the ConfigureApplicationContainer method.

// Configure bus
var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.Nancy.Sender");
busConfiguration.UsePersistence<InMemoryPersistence>();
busConfiguration.EnableInstallers();

// Create bus instance
var bus = Bus.CreateSendOnly(busConfiguration);

// Register bus instance
container.Register<ISendOnlyBus>(bus);

Injection into the module

The endpoint instance is injected into the SendMessageModule at construction time.

public class SendMessageModule :
    NancyModule
{
    private readonly ISendOnlyBus bus;

    public SendMessageModule(ISendOnlyBus bus) : base("/sendmessage")
    {
        this.bus = bus;

        this.Get["/"] = r =>
        {
            var message = new MyMessage();
            bus.Send("Samples.Nancy.Endpoint", message);
            return "Message sent to endpoint";
        };
    }
}

Related Articles


Last modified