WCF December 5, 2010

Multicast Message Routing with WCF 3.5

Implementing distributed architectures with multicast routing in Windows Communication Foundation 3.5. This pattern enables content-based routing where a single message is duplicated and forwarded to multiple backend servers based on SOAP header information.

The Multicast Router Pattern

For distributed architecture scenarios requiring multicast message routing, WCF 3.5 provides the infrastructure to create intelligent routers that can duplicate and forward messages to multiple destinations.

Use Case

This pattern is particularly useful for scenarios such as:

  • Replicating data to multiple geographic locations
  • Broadcasting events to multiple backend systems
  • Creating redundant storage across multiple servers
  • Implementing disaster recovery with live data mirroring

Implementation

The service contract defines a router interface that accepts any message and returns a response:

[ServiceContract(Namespace = "http://servicios.cliente/2009/06", Name = "IRouter")]
public interface IServiceRouter
{
    [OperationContract(ReplyAction = "*", Action = "*")]
    Message ForwardMessage(Message message);
}

The key configuration element is AddressFilterMode = AddressFilterMode.Any, which allows the service to accept messages regardless of their destination address. This enables the router to act as a transparent intermediary.

Content-Based Routing

The router implementation uses a content-based routing strategy, examining SOAP headers to determine where to forward message copies:

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
class ServiceRouter : IServiceRouter
{
    public Message ForwardMessage(Message message)
    {
        // Create buffered copy to allow multiple reads
        MessageBuffer messageBuffer = message.CreateBufferedCopy(int.MaxValue);
        Message messageCopy1 = messageBuffer.CreateMessage();
        Message messageCopy2 = messageBuffer.CreateMessage();

        // Route to local server
        int headerLocalIndex = message.Headers.FindHeader("Key",
            "http://serviciosproduccionCP");
        if (headerLocalIndex != -1)
        {
            string localHost = message.Headers.GetHeader<string>(headerLocalIndex);
            ChannelFactory<IServiceRouter> client =
                new ChannelFactory<IServiceRouter>(localHost);
            Message localServer = (client.CreateChannel()).ForwardMessage(messageCopy1);
        }

        // Route to remote server
        int headerRemoteIndex = message.Headers.FindHeader("Key",
            "http://serviciosproduccionBogota");
        if (headerRemoteIndex != -1)
        {
            string remoteHost = message.Headers.GetHeader<string>(headerRemoteIndex);
            ChannelFactory<IServiceRouter> client =
                new ChannelFactory<IServiceRouter>(remoteHost);
            Message remoteServer = (client.CreateChannel()).ForwardMessage(messageCopy2);
            return remoteServer;
        }

        throw new InvalidOperationException("Required routing headers not found");
    }
}

Key Concepts

Message Buffering

The CreateBufferedCopy() method is crucial for multicast scenarios. WCF messages can only be read once by default, so creating a buffered copy allows us to generate multiple message instances from the same source.

SOAP Header Inspection

The router examines SOAP headers to determine routing destinations. This content-based routing approach allows clients to control message distribution by setting appropriate headers, enabling flexible multicast patterns.

Dynamic Channel Creation

Using ChannelFactory<T> at runtime allows the router to dynamically create connections to backend services based on header values, avoiding the need to hardcode destination endpoints.

Production Considerations

Performance & Reliability

  • Channel Pooling: Consider pooling ChannelFactory instances to avoid creation overhead
  • Error Handling: Implement retry logic and circuit breakers for failed destinations
  • Async Operations: Use async patterns to avoid blocking during multicast
  • Monitoring: Add instrumentation to track routing success rates and latencies

Modern Alternatives

Note: This article from 2010 demonstrates WCF 3.5 routing patterns. Modern distributed systems typically use message brokers (RabbitMQ, Kafka, Azure Service Bus) or service meshes for multicast scenarios, which provide better scalability, reliability, and operational tooling. However, the fundamental concepts of content-based routing and message buffering remain relevant across all messaging platforms.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy.