Routing Messages Edit on GitHub


When you publish a message using IServiceBus 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 one of the SendMessage**** methods as shown below:


public class StaticRoutingApp : JasperRegistry
{
    public StaticRoutingApp(AppSettings settings)
    {
        // Explicitly add a single message type
        Publish.Message<PingMessage>()
            .To(settings.Transactions);

        // Publish any message type contained in the assembly
        // to this channel, by supplying a type contained
        // within that assembly
        Publish.MessagesFromAssemblyContaining<PingMessage>()
            .To(settings.Transactions);

        // Publish any message type contained in the named
        // assembly to this channel
        Publish.MessagesFromAssembly(Assembly.Load(new AssemblyName("MyMessageLibrary")))
            .To(settings.Transactions);

        // Publish any message type contained in the
        // namespace given to this channel
        Publish.MessagesFromNamespace("MyMessageLibrary")
            .To(settings.Transactions);

        // Publish any message type contained in the namespace
        // of the type to this channel
        Publish.MessagesFromNamespaceContaining<PingMessage>()
            .To(settings.Transactions);
    }
}

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.

See also the

Unknown topic key 'documentation/messaging/routing/subscriptions' -- CTRL+SHIFT+R to force refresh the topic tree