TransportOperation RouteThroughLocalEndpointInstance(RoutingStrategy routingStrategy, IRoutingContext context)
    {
        Dictionary <string, string> headers = new Dictionary <string, string>(context.Message.Headers);
        AddressTag        originalTag       = routingStrategy.Apply(headers);
        UnicastAddressTag unicastTag        = originalTag as UnicastAddressTag;

        if (unicastTag == null)
        {
            MulticastAddressTag multicastTag = originalTag as MulticastAddressTag;
            if (multicastTag != null)
            {
                headers["$.store-and-forward.eventtype"] = multicastTag.MessageType.AssemblyQualifiedName;
            }
            else
            {
                throw new Exception("Unsupported type of address tag: " + originalTag.GetType().FullName);
            }
        }
        else
        {
            headers["$.store-and-forward.destination"] = unicastTag.Destination;
        }
        OutgoingMessage message = new OutgoingMessage(context.Message.MessageId, headers, context.Message.Body);

        return(new TransportOperation(message, new UnicastAddressTag(localAddress), DispatchConsistency.Default, context.Extensions.GetDeliveryConstraints()));
    }
        static void SerializeAddressTag(AddressTag addressTag, Dictionary <string, string> options)
        {
            if (addressTag is UnicastAddressTag direct)
            {
                options["Destination"] = direct.Destination;
                return;
            }

            if (addressTag is MulticastAddressTag)
            {
                throw new Exception("Token-based deduplication cannot be used with multicast routing.");
            }

            throw new Exception($"Unknown routing strategy {addressTag.GetType().FullName}");
        }
        static void SerializeRoutingStrategy(AddressTag addressTag, Dictionary <string, string> options)
        {
            if (addressTag is MulticastAddressTag indirect)
            {
                options["EventType"] = indirect.MessageType.AssemblyQualifiedName;
                return;
            }

            if (addressTag is UnicastAddressTag direct)
            {
                options["Destination"] = direct.Destination;
                return;
            }

            throw new Exception($"Unknown routing strategy {addressTag.GetType().FullName}");
        }