Routing Messages


When you publish a message using IMessageContext without explicitly setting the Uri of the desired destination, Jasper has to invoke the known message routing rules and dynamic subscriptions to figure out which locations should receive the message. Consider this code that publishes a PingMessage:


public class SendingExample
{
    public async Task SendPingsAndPongs(IMessageContext bus)
    {
        // Publish a message
        await bus.Send(new PingMessage());
    }
}

To route PingMessage to a channel, we can apply static message routing rules by using the Endpoint.Publish() method as shown below:


public class StaticPublishingRulesApp : JasperOptions
{
    public StaticPublishingRulesApp()
    {
        Endpoints.Publish(x =>
        {
            // Apply as many message matching
            // rules as you need

            // Specific message types
            x.Message<PingMessage>();
            x.Message<Message1>();

            // All types in a certain assembly
            x.MessagesFromAssemblyContaining<PingMessage>();

            // or this
            x.MessagesFromAssembly(typeof(PingMessage).Assembly);

            // or by namespace
            x.MessagesFromNamespace("MyMessageLibrary");
            x.MessagesFromNamespaceContaining<PingMessage>();

            // Express the subscribers
            x.ToPort(1111);
            x.ToPort(2222);
        });

        // Or you just send all messages to a certain endpoint
        Endpoints.PublishAllMessages().ToPort(3333);
    }

Do note that doing the message type filtering by namespace will also include child namespaces. In our own usage we try to rely on either namespace rules or by using shared message assemblies.