Sending from a Nancy module

Component: NServiceBus
NuGet Package NServiceBus (6.x)

Initialize the Nancy web endpoint

Open Bootstraper.cs and look at the ConfigureApplicationContainer method.

// Configure endpoint
var endpointConfiguration = new EndpointConfiguration("Samples.Nancy.Sender");
var transport = endpointConfiguration.UseTransport<LearningTransport>();
endpointConfiguration.UsePersistence<LearningPersistence>();
endpointConfiguration.SendOnly();

// Define routing
var routing = transport.Routing();
routing.RouteToEndpoint(
    assembly: typeof(MyMessage).Assembly,
    destination: "Samples.Nancy.Endpoint");

// Start endpoint instance
var endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

// Register endpoint instance
container.Register<IMessageSession>(endpointInstance);

Injection into the module

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

public class SendMessageModule :
    NancyModule
{
    private readonly IMessageSession messageSession;

    public SendMessageModule(IMessageSession messageSession) : base("/sendmessage")
    {
        this.messageSession = messageSession;

        this.Get["/", true] = async (r, c) =>
        {
            var message = new MyMessage();
            await messageSession.Send(message)
                .ConfigureAwait(false);
            return "Message sent to endpoint";
        };
    }
}

Related Articles


Last modified