예제 #1
0
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var forwardedHeaders = new Dictionary <string, string>(context.Headers);

        string replyTo = null;

        if (!forwardedHeaders.TryGetValue(Headers.CorrelationId, out var correlationId))
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        correlationId.DecodeTLV((t, v) =>
        {
            if (t == "reply-to")
            {
                replyTo = v;
            }
            if (t == "id")
            {
                forwardedHeaders[Headers.CorrelationId] = v;
            }
        });

        if (replyTo == null)
        {
            throw new UnforwardableMessageException("The reply message does not contain \'reply-to\' correlation parameter required to route the message.");
        }

        var outgoingMessage = new OutgoingMessage(context.MessageId, forwardedHeaders, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(replyTo));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var publisherInstances = endpointInstances.FindInstances(publisherEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var subscriptionMessage = ControlMessageFactory.Create(intent);

            subscriptionMessage.Headers[Headers.SubscriptionMessageType]          = messageType;
            subscriptionMessage.Headers[Headers.ReplyToAddress]                   = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberTransportAddress]       = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberEndpoint]               = dispatcher.EndpointName;
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
            subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
            subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
예제 #3
0
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding)
    {
        var typeFullName = messageType.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).First();

        if (!forwarding.PublisherTable.TryGetValue(typeFullName, out var nextHopEndpoint))
        {
            return;
        }

        var subscriptionMessage = ControlMessageFactory.Create(intent);

        if (publisherEndpoint != null)
        {
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
        }
        subscriptionMessage.Headers[Headers.SubscriptionMessageType]    = messageType;
        subscriptionMessage.Headers[Headers.ReplyToAddress]             = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberTransportAddress] = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberEndpoint]         = dispatcher.EndpointName;
        subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
        subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

        var publisherInstances = endpointInstances.FindInstances(nextHopEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
예제 #4
0
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        string replyTo = null;

        if (!context.Headers.TryGetValue(Headers.CorrelationId, out string correlationId))
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        correlationId.DecodeTLV((t, v) =>
        {
            if (t == "reply-to")
            {
                replyTo = v;
            }
        });

        if (replyTo == null)
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(replyTo));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
    protected override async Task <bool> Terminate(PostroutingContext context)
    {
        if (random.Next(2) == 0)
        {
            await dispatcher.Dispatch(new TransportOperations(Copy(context.Messages)), context.Get <TransportTransaction>(), context)
            .ConfigureAwait(false);

            return(true);
        }
        return(false);
    }
    protected override async Task <bool> Terminate(PostroutingContext context)
    {
        foreach (var operation in context.Messages)
        {
            AddTrace(operation);
        }
        await dispatcher.Dispatch(new TransportOperations(context.Messages), context.Get <TransportTransaction>(), context)
        .ConfigureAwait(false);

        return(true);
    }
예제 #7
0
    public Task Route(MessageContext context, IRawEndpoint dispatcher)
    {
        if (!context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes))
        {
            throw new UnforwardableMessageException("Message need to have 'NServiceBus.EnclosedMessageTypes' header in order to be routed.");
        }
        var types = messageTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        var addressTag      = new MulticastAddressTag(typeGenerator.GetType(types.First()));
        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, addressTag);

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
        public async Task OnMessage(MessageContext context, IDispatchMessages _)
        {
            if (!context.Headers.TryGetValue(Headers.MessageIntent, out var messageIntentString) ||
                !Enum.TryParse(messageIntentString, true, out MessageIntentEnum messageIntent))
            {
                return;
            }

            var request = new OutgoingMessage(
                messageId: context.MessageId,
                headers: context.Headers,
                body: context.Body);

            if (context.Headers.ContainsKey(Headers.OriginatingEndpoint))
            {
                context.Headers[Headers.OriginatingEndpoint] = _destinationEndpointName;
            }

            TransportOperation operation;

            switch (messageIntent)
            {
            case MessageIntentEnum.Send:
                operation = new TransportOperation(
                    request,
                    new UnicastAddressTag(GetDestination(context)));
                break;

            case MessageIntentEnum.Publish:
                operation = new TransportOperation(
                    request,
                    new MulticastAddressTag(GetMessageType(context)));
                break;

            default:
                return;
            }

            var transaction = new TransportTransaction();

            // Participate in a tx scope if its active
            transaction.Set(Transaction.Current);

            await _destinationEndpoint.Dispatch(
                outgoingMessages : new TransportOperations(operation),
                transaction : transaction,
                context : new ContextBag())
            .ConfigureAwait(false);
        }
예제 #9
0
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        string destinationEndpoint;

        if (!context.Headers.TryGetValue("NServiceBus.Bridge.DestinationEndpoint", out destinationEndpoint))
        {
            throw new UnforwardableMessageException("Sent message does not contain the 'NServiceBus.Bridge.DestinationEndpoint' header.");
        }
        var address = SelectDestinationAddress(destinationEndpoint, i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i)));

        context.Headers[Headers.ReplyToAddress] = dispatcher.TransportAddress;

        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(address));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
예제 #10
0
    public async Task Route(MessageContext context, IRawEndpoint dispatcher)
    {
        if (!context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes))
        {
            throw new UnforwardableMessageException("Message need to have 'NServiceBus.EnclosedMessageTypes' header in order to be routed.");
        }
        var types       = messageTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        var typeObjects = types.Select(t => new MessageType(t));

        var subscribers = await subscriptionStorage.GetSubscriberAddressesForMessage(typeObjects, new ContextBag()).ConfigureAwait(false);

        var destinations    = SelectDestinationsForEachEndpoint(subscribers, context);
        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operations      = destinations.Select(x => new TransportOperation(outgoingMessage, new UnicastAddressTag(x)));

        await dispatcher.Dispatch(new TransportOperations(operations.ToArray()), context.TransportTransaction, context.Extensions).ConfigureAwait(false);
    }
예제 #11
0
    Task Forward(MessageContext context, IRawEndpoint dispatcher, string destinationEndpoint, string sourcePort)
    {
        var forwardedHeaders = new Dictionary <string, string>(context.Headers);

        var address = SelectDestinationAddress(destinationEndpoint, i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i)));

        if (forwardedHeaders.TryGetValue(Headers.ReplyToAddress, out var replyToHeader) &&
            forwardedHeaders.TryGetValue(Headers.CorrelationId, out var correlationId))
        {
            // pipe-separated TLV format
            var newCorrelationId = $"id|{correlationId.Length}|{correlationId}|reply-to|{replyToHeader.Length}|{replyToHeader}";
            if (sourcePort != null)
            {
                newCorrelationId += $"|port|{sourcePort.Length}|{sourcePort}";
            }
            forwardedHeaders[Headers.CorrelationId] = newCorrelationId;
        }
        forwardedHeaders[Headers.ReplyToAddress] = dispatcher.TransportAddress;

        var outgoingMessage = new OutgoingMessage(context.MessageId, forwardedHeaders, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(address));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
예제 #12
0
    public static Task Publish(this IRawEndpoint endpoint, Type eventType, Dictionary <string, string> headers, byte[] body)
    {
        var op = new TransportOperation(new OutgoingMessage(Guid.NewGuid().ToString(), headers, body), new MulticastAddressTag(eventType));

        return(endpoint.Dispatch(new TransportOperations(op), new TransportTransaction(), new ContextBag()));
    }
예제 #13
0
    public static Task Send(this IRawEndpoint endpoint, string destination, Dictionary <string, string> headers, byte[] body)
    {
        var op = new TransportOperation(new OutgoingMessage(Guid.NewGuid().ToString(), headers, body), new UnicastAddressTag(destination));

        return(endpoint.Dispatch(new TransportOperations(op), new TransportTransaction(), new ContextBag()));
    }