Esempio n. 1
0
        public static Task Reply(IBehaviorContext context, object message, ReplyOptions options)
        {
            var mapper      = context.Extensions.Get <IMessageMapper>();
            var messageType = mapper.GetMappedTypeFor(message.GetType());

            return(ReplyMessage(context, messageType, message, options));
        }
Esempio n. 2
0
        public void ShouldShallowCloneContext()
        {
            var message = new OutgoingLogicalMessage(typeof(object), new object());
            var options = new ReplyOptions();

            options.Context.Set("someKey", "someValue");

            var testee = new OutgoingReplyContext(message, options, new RootContext(null, null, null));

            testee.Extensions.Set("someKey", "updatedValue");
            testee.Extensions.Set("anotherKey", "anotherValue");

            string value;
            string anotherValue;

            options.Context.TryGet("someKey", out value);
            Assert.AreEqual("someValue", value);
            Assert.IsFalse(options.Context.TryGet("anotherKey", out anotherValue));
            string updatedValue;
            string anotherValue2;

            testee.Extensions.TryGet("someKey", out updatedValue);
            testee.Extensions.TryGet("anotherKey", out anotherValue2);
            Assert.AreEqual("updatedValue", updatedValue);
            Assert.AreEqual("anotherValue", anotherValue2);
        }
Esempio n. 3
0
        /// <summary>
        /// Instructs the receiver to route the reply for this message to any instance of this endpoint.
        /// </summary>
        /// <param name="options">Option being extended.</param>
        public static void RouteReplyToAnyInstance(this ReplyOptions options)
        {
            Guard.AgainstNull(nameof(options), options);

            options.Context.GetOrCreate <ApplyReplyToAddressBehavior.State>()
            .Option = ApplyReplyToAddressBehavior.RouteOption.RouteReplyToAnyInstanceOfThisEndpoint;
        }
Esempio n. 4
0
        public static DeliveryOptions ToDeliveryOptions(this Dictionary <string, string> options)
        {
            var operation = options["Operation"].ToLower();

            switch (operation)
            {
            case "publish":
                return(new PublishOptions(Type.GetType(options["EventType"]))
                {
                    ReplyToAddress = Address.Parse(options["ReplyToAddress"])
                });

            case "send":
            case "audit":
                var sendOptions = new SendOptions(options["Destination"]);

                ApplySendOptionSettings(sendOptions, options);

                return(sendOptions);

            case "reply":
                var replyOptions = new ReplyOptions(Address.Parse(options["Destination"]), options["CorrelationId"])
                {
                    ReplyToAddress = Address.Parse(options["ReplyToAddress"])
                };
                ApplySendOptionSettings(replyOptions, options);

                return(replyOptions);

            default:
                throw new Exception("Unknown operation: " + operation);
            }
        }
Esempio n. 5
0
                public async Task Handle(Message message, IMessageHandlerContext context)
                {
                    var batchPolicy = Policy
                                      .Handle <Exception>(ex => true)
                                      .RetryAsync(1, (exception, retryAttempt, context) =>
                    {
                        _numberOfOverriddenBatchPollyRetries++;
                    });

                    context.OverrideBatchDispatchRetryPolicy(batchPolicy);

                    var immediatePolicy = Policy
                                          .Handle <Exception>(ex => true)
                                          .RetryAsync(1, (exception, retryAttempt, context) =>
                    {
                        _numberOfOverriddenImmediatePollyRetries++;
                    });

                    context.OverrideBatchDispatchRetryPolicy(batchPolicy);
                    context.OverrideImmediateDispatchRetryPolicy(immediatePolicy);

                    var options = new ReplyOptions();

                    options.RequireImmediateDispatch();
                    await context.Reply(new ReplyMessage(), options);

                    await context.Reply(new AnotherReplyMessage());
                }
Esempio n. 6
0
        /// <summary>
        /// Sends the <paramref name="message" /> using the bus to the endpoint that caused this saga to start.
        /// </summary>
        protected Task ReplyToOriginator(IMessageHandlerContext context, object message, IReadOnlyDictionary <string, string> outgoingHeaders = null)
        {
            if (string.IsNullOrEmpty(Entity.Originator))
            {
                throw new Exception("Entity.Originator cannot be null. Perhaps the sender is a SendOnly endpoint.");
            }

            var options = new ReplyOptions();

            foreach (var keyValuePair in outgoingHeaders ?? Enumerable.Empty <KeyValuePair <string, string> >())
            {
                options.OutgoingHeaders.Add(keyValuePair.Key, keyValuePair.Value);
            }

            options.SetDestination(Entity.Originator);
            context.Extensions.Set(new AttachCorrelationIdBehavior.State {
                CustomCorrelationId = Entity.OriginalMessageId
            });

            //until we have metadata we just set this to null to avoid our own saga id being set on outgoing messages since
            //that would cause the saga that started us (if it was a saga) to not be found. When we have metadata available in the future we'll set the correct id and type
            // and get true auto correlation to work between sagas
            options.Context.Set(new PopulateAutoCorrelationHeadersForRepliesBehavior.State
            {
                SagaTypeToUse = null,
                SagaIdToUse   = null
            });

            return(context.Reply(message, options));
        }
        /// <summary>
        /// Returns the destination configured by <see cref="SetDestination(ReplyOptions, string)" />.
        /// </summary>
        /// <param name="options">Option being extended.</param>
        /// <returns>The specified destination address or <c>null</c> when no destination was specified.</returns>
        public static string GetDestination(this ReplyOptions options)
        {
            Guard.AgainstNull(nameof(options), options);

            options.Context.TryGet(out UnicastReplyRouterConnector.State state);
            return(state?.ExplicitDestination);
        }
        public void ReplyOptions_GetCorrelationId_Should_Return_Null_When_No_CorrelationId_Configured()
        {
            var options = new ReplyOptions();

            var correlationId = options.GetCorrelationId();

            Assert.IsNull(correlationId);
        }
Esempio n. 9
0
            public Task Handle(StartSagaAndReply message, IMessageHandlerContext context)
            {
                var options = new ReplyOptions();

                options.SetDestination("AnotherDestination");

                return(context.Reply(new UnexpectedReplyMessage(), options));
            }
                public Task Handle(Request message, IMessageHandlerContext context)
                {
                    var options = new ReplyOptions();

                    options.SetHeader("NServiceBus.RabbitMQ.CallbackQueue", Conventions.EndpointNamingConvention(typeof(ReceivingEndpoint)));

                    return(context.Reply(new Reply(), options));
                }
        public void ReplyOptions_IsRoutingReplyToAnyInstance_Should_Return_True_When_Routing_Reply_To_Any_Instance()
        {
            var options = new ReplyOptions();

            options.RouteReplyToAnyInstance();

            Assert.IsTrue(options.IsRoutingReplyToAnyInstance());
        }
        public void ReplyOptions_GetDestination_Should_Return_Null_When_No_Destination_Configured()
        {
            var options = new ReplyOptions();

            var destination = options.GetDestination();

            Assert.IsNull(destination);
        }
Esempio n. 13
0
        /// <summary>
        /// Allows the target endpoint instance for this reply to set. If not used the reply will be sent to the `ReplyToAddress`
        /// of the incoming message.
        /// </summary>
        /// <param name="options">Option being extended.</param>
        /// <param name="destination">The new target address.</param>
        public static void SetDestination(this ReplyOptions options, string destination)
        {
            Guard.AgainstNull(nameof(options), options);
            Guard.AgainstNullAndEmpty(nameof(destination), destination);

            options.Context.GetOrCreate <UnicastReplyRouterConnector.State>()
            .ExplicitDestination = destination;
        }
        public void ReplyOptions_GetDestination_Should_Return_Null_When_No_Destination_Configured()
        {
            var options = new ReplyOptions();

            var destination = options.GetDestination();

            Assert.IsNull(destination);
        }
Esempio n. 15
0
                public Task Handle(Request message, IMessageHandlerContext context)
                {
                    var replyOptions = new ReplyOptions();

                    replyOptions.SetHeader("MyHeader", "SomeValue");

                    return(context.Reply(new ResponseToBeAudited(), replyOptions));
                }
                public Task Handle(Message message, IMessageHandlerContext context)
                {
                    var options = new ReplyOptions();

                    options.RequireImmediateDispatch();

                    return(context.Reply(new ReplyMessage(), options));
                }
Esempio n. 17
0
        /// <summary>
        /// Allows users to set a custom correlation id.
        /// </summary>
        /// <param name="options">Options being extended.</param>
        /// <param name="correlationId">The custom correlation id.</param>
        public static void SetCorrelationId(this ReplyOptions options, string correlationId)
        {
            Guard.AgainstNull(nameof(options), options);
            Guard.AgainstNullAndEmpty(nameof(correlationId), correlationId);

            options.Context.GetOrCreate <AttachCorrelationIdBehavior.State>()
            .CustomCorrelationId = correlationId;
        }
Esempio n. 18
0
        public void ReplyOptions_GetCorrelationId_Should_Return_Null_When_No_CorrelationId_Configured()
        {
            var options = new ReplyOptions();

            var correlationId = options.GetCorrelationId();

            Assert.IsNull(correlationId);
        }
                public override Task Invoke(IIncomingPhysicalMessageContext context, Func <Task> next)
                {
                    var replyOptions = new ReplyOptions();

                    replyOptions.SetDestination(context.Message.Headers[Headers.ReplyToAddress].Replace(SenderAlias, Utilities.GetEnvConfiguredConnectionString()));
                    replyOptions.SetHeader("reply-message-as-expected", "OK");

                    return(context.Reply(new MyMessageReply(), replyOptions));
                }
    public Task Handle(SendMessage message, IMessageHandlerContext context)
    {
        var replyOptions       = new ReplyOptions();
        var outgoingAttachment = replyOptions.Attachments();
        var incomingAttachment = context.Attachments();

        outgoingAttachment.Add(incomingAttachment.GetStream);
        return(context.Reply(new ReplyMessage(), replyOptions));
    }
Esempio n. 21
0
        /// <summary>
        /// Retrieves the correlation id specified by the user by using
        /// <see cref="SetCorrelationId(NServiceBus.ReplyOptions,string)" />.
        /// </summary>
        /// <param name="options">Options being extended.</param>
        /// <returns>The configured correlation id or <c>null</c> when no correlation id was configured.</returns>
        public static string GetCorrelationId(this ReplyOptions options)
        {
            Guard.AgainstNull(nameof(options), options);

            AttachCorrelationIdBehavior.State state;
            options.Context.TryGet(out state);

            return(state?.CustomCorrelationId);
        }
Esempio n. 22
0
        public override async Task Reply(object message, ReplyOptions options)
        {
            Guard.AgainstNull(message, nameof(message));
            Guard.AgainstNull(options, nameof(options));
            ValidateHasRun();
            await TestContextValidator.Validate(message, options, Builder);

            await base.Reply(message, options);
        }
        async Task ReplySendReplyTo(IMessageHandlerContext context)
        {
            #region BasicReplyReplyToDestination
            ReplyOptions options = new ReplyOptions();
            options.RouteReplyTo("MyDestination");
            await context.Reply(new MyMessage(), options);

            #endregion
        }
        public void ReplyOptions_GetReplyToRoute_Should_Return_Configured_Reply_Route()
        {
            const string expectedReplyToAddress = "custom replyTo address";
            var          options = new ReplyOptions();

            options.RouteReplyTo(expectedReplyToAddress);

            Assert.AreEqual(expectedReplyToAddress, options.GetReplyToRoute());
        }
        async Task ReplySendReplyToAnyInstance(IMessageHandlerContext context)
        {
            #region BasicReplyReplyToAnyInstance
            ReplyOptions options = new ReplyOptions();
            options.RouteReplyToAnyInstance();
            await context.Reply(new MyMessage(), options);

            #endregion
        }
Esempio n. 26
0
                public async Task Handle(Message message, IMessageHandlerContext context)
                {
                    var options = new ReplyOptions();

                    options.RequireImmediateDispatch();
                    await context.Reply(new ReplyMessage(), options);

                    await context.Reply(new AnotherReplyMessage());
                }
Esempio n. 27
0
        /// <summary>
        /// Instructs the receiver to route the reply to specified address.
        /// </summary>
        /// <param name="options">Option being extended.</param>
        /// <param name="address">Reply destination.</param>
        public static void RouteReplyTo(this ReplyOptions options, string address)
        {
            Guard.AgainstNull(nameof(options), options);
            Guard.AgainstNull(nameof(address), address);

            var state = options.Context.GetOrCreate <ApplyReplyToAddressBehavior.State>();

            state.Option = ApplyReplyToAddressBehavior.RouteOption.ExplicitReplyDestination;
            state.ExplicitDestination = address;
        }
Esempio n. 28
0
        public OutgoingReplyContext(OutgoingLogicalMessage message, ReplyOptions options, IBehaviorContext parentContext)
            : base(options.MessageId, new Dictionary <string, string>(options.OutgoingHeaders), parentContext)
        {
            Message = message;
            Guard.AgainstNull(nameof(parentContext), parentContext);
            Guard.AgainstNull(nameof(message), message);
            Guard.AgainstNull(nameof(options), options);

            parentContext.Extensions.Merge(options.Context);
        }
        public OutgoingReplyContext(OutgoingLogicalMessage message, ReplyOptions options, IBehaviorContext parentContext)
            : base(options.MessageId, new Dictionary<string, string>(options.OutgoingHeaders), parentContext)
        {
            Message = message;
            Guard.AgainstNull(nameof(parentContext), parentContext);
            Guard.AgainstNull(nameof(message), message);
            Guard.AgainstNull(nameof(options), options);

            parentContext.Extensions.Merge(options.Context);
        }
        public void ReplyOptions_GetCorrelationId_Should_Return_Configured_CorrelationId()
        {
            const string expectedCorrelationId = "custom correlation id";
            var options = new ReplyOptions();
            options.SetCorrelationId(expectedCorrelationId);

            var correlationId = options.GetCorrelationId();

            Assert.AreEqual(expectedCorrelationId, correlationId);
        }
    public Task Handle(SendMessage message, IMessageHandlerContext context)
    {
        var replyOptions = new ReplyOptions();
        var replyMessage = new ReplyMessage
        {
            Blob = new DataBusProperty <byte[]>(message.Blob.Value)
        };

        return(context.Reply(replyMessage, replyOptions));
    }
        public void ReplyOptions_GetDestination_Should_Return_Configured_Destination()
        {
            const string expectedDestination = "custom reply destination";
            var options = new ReplyOptions();
            options.SetDestination(expectedDestination);

            var destination = options.GetDestination();

            Assert.AreEqual(expectedDestination, destination);
        }
Esempio n. 33
0
        public async Task Handle(RequestMessage message, IMessageHandlerContext context)
        {
            string header = context.MessageHeaders["MyHeaderKey"];

            ResponseMessage responseMessage = new ResponseMessage();

            ReplyOptions options = new ReplyOptions();

            options.SetHeader("MyHeaderKey", header);
            await context.Reply(responseMessage, options);
        }
Esempio n. 34
0
        public Task Handle(RequestMessage message, IMessageHandlerContext context)
        {
            var header = context.MessageHeaders["MyHeaderKey"];

            var responseMessage = new ResponseMessage();

            var options = new ReplyOptions();

            options.SetHeader("MyHeaderKey", header);
            return(context.Reply(responseMessage, options));
        }
        public void ReplyOptions_GetDestination_Should_Return_Configured_Destination()
        {
            const string expectedDestination = "custom reply destination";
            var          options             = new ReplyOptions();

            options.SetDestination(expectedDestination);

            var destination = options.GetDestination();

            Assert.AreEqual(expectedDestination, destination);
        }
        public async Task Reply_ShouldContainMessageInRepliedMessages()
        {
            var context = new TestableMessageHandlerContext();
            var messageInstance = new TestMessage();
            var publishOptions = new ReplyOptions();

            await context.Reply(messageInstance, publishOptions);

            Assert.AreEqual(1, context.RepliedMessages.Length);
            Assert.AreSame(messageInstance, context.RepliedMessages[0].Message);
            Assert.AreSame(publishOptions, context.RepliedMessages[0].Options);
        }
        public void ShouldShallowCloneHeaders()
        {
            var message = new OutgoingLogicalMessage(typeof(object), new object());
            var options = new ReplyOptions();
            options.SetHeader("someHeader", "someValue");

            var testee = new OutgoingReplyContext(message, options, new RootContext(null, null, null));
            testee.Headers["someHeader"] = "updatedValue";
            testee.Headers["anotherHeader"] = "anotherValue";

            Assert.AreEqual("someValue", options.OutgoingHeaders["someHeader"]);
            Assert.IsFalse(options.OutgoingHeaders.ContainsKey("anotherHeader"));
            Assert.AreEqual("updatedValue", testee.Headers["someHeader"]);
            Assert.AreEqual("anotherValue", testee.Headers["anotherHeader"]);
        }
        public async Task Should_use_explicit_route_for_replies_if_present()
        {
            var behavior = new UnicastReplyRouterConnector();
            var options = new ReplyOptions();

            options.SetDestination("CustomReplyToAddress");

            var context = CreateContext(new OutgoingLogicalMessage(typeof(MyReply), new MyReply()));
            context.Extensions = options.Context;

            UnicastAddressTag addressTag = null;
            await behavior.Invoke(context, c =>
            {
                addressTag = (UnicastAddressTag) c.RoutingStrategies.Single().Apply(new Dictionary<string, string>());
                return TaskEx.CompletedTask;
            });

            Assert.AreEqual("CustomReplyToAddress", addressTag.Destination);
        }
        public void ShouldShallowCloneContext()
        {
            var message = new OutgoingLogicalMessage(typeof(object), new object());
            var options = new ReplyOptions();
            options.Context.Set("someKey", "someValue");

            var testee = new OutgoingReplyContext(message, options, new RootContext(null, null, null));
            testee.Extensions.Set("someKey", "updatedValue");
            testee.Extensions.Set("anotherKey", "anotherValue");

            string value;
            string anotherValue;
            options.Context.TryGet("someKey", out value);
            Assert.AreEqual("someValue", value);
            Assert.IsFalse(options.Context.TryGet("anotherKey", out anotherValue));
            string updatedValue;
            string anotherValue2;
            testee.Extensions.TryGet("someKey", out updatedValue);
            testee.Extensions.TryGet("anotherKey", out anotherValue2);
            Assert.AreEqual("updatedValue", updatedValue);
            Assert.AreEqual("anotherValue", anotherValue2);
        }
        public static DeliveryOptions ToDeliveryOptions(this Dictionary<string, string> options)
        {
            var operation = options["Operation"].ToLower();

            switch (operation)
            {
                case "publish":
                    return new PublishOptions(Type.GetType(options["EventType"]))
                    {
                        ReplyToAddress = Address.Parse(options["ReplyToAddress"])
                    };

                case "send":
                case "audit":
                    var sendOptions = new SendOptions(options["Destination"]);

                    ApplySendOptionSettings(sendOptions, options);

                    return sendOptions;

                case "reply":
                    var replyOptions = new ReplyOptions(Address.Parse(options["Destination"]), options["CorrelationId"])
                    {
                        ReplyToAddress = Address.Parse(options["ReplyToAddress"])
                    };
                    ApplySendOptionSettings(replyOptions, options);

                    return replyOptions;

                default:
                    throw new Exception("Unknown operation: " + operation);
            }


        }
        public void ReplyOptions_GetReplyToRoute_Should_Return_Null_When_No_Route_Configured()
        {
            var options = new ReplyOptions();

            Assert.IsNull(options.GetReplyToRoute());
        }
        public void ReplyOptions_GetReplyToRoute_Should_Return_Configured_Reply_Route()
        {
            const string expectedReplyToAddress = "custom replyTo address";
            var options = new ReplyOptions();

            options.RouteReplyTo(expectedReplyToAddress);

            Assert.AreEqual(expectedReplyToAddress, options.GetReplyToRoute());
        }
        public void ReplyOptions_IsRoutingReplyToAnyInstance_Should_Return_False_When_Not_Routing_Reply_To_Any_Instance()
        {
            var options = new ReplyOptions();

            Assert.IsFalse(options.IsRoutingReplyToAnyInstance());
        }
        public void ReplyOptions_IsRoutingReplyToAnyInstance_Should_Return_True_When_Routing_Reply_To_Any_Instance()
        {
            var options = new ReplyOptions();

            options.RouteReplyToAnyInstance();

            Assert.IsTrue(options.IsRoutingReplyToAnyInstance());
        }