public void Test_Dispose()
        {
            TestMessage msg;
            TestMessage msg1 = new TestMessage();
            TestMessage msg2 = new TestMessage();

            using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext())
            {
                outerContext.AddOrUpdateMessage("Channel1", msg1, null);
                outerContext.AddOrUpdateMessage("Channel2", msg2, null);

                Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg");

                Assert.AreSame(msg, msg1);

                Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg");

                Assert.AreSame(msg, msg2);

                outerContext.Dispose();

                Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.False, "Outer context contains msg");
                Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.False, "Outer context contains msg");
            }
        }
        public void Test_Dispose_DifferentThread()
        {
            TestMessage msg;
            TestMessage msg1 = new TestMessage();
            TestMessage msg2 = new TestMessage();

            using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext())
            {
                outerContext.AddOrUpdateMessage("Channel1", msg1, null);
                outerContext.AddOrUpdateMessage("Channel2", msg2, null);

                Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg");

                Assert.AreSame(msg, msg1);

                Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg");

                Assert.AreSame(msg, msg2);

                // Call the dispose on a different thread.
                // This tests the Begin Request/End Request creation and disposal
                Thread tSetup = new Thread(new ThreadStart(() => { outerContext.Dispose(); }));
                tSetup.Start();
                tSetup.Join();

                Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.False, "Outer context contains msg");
                Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.False, "Outer context contains msg");
            }
        }
        public void Test_AttachedContext_Does_Not_Flush_On_Dispose()
        {
            TestMessage msg;
            TestMessage msgAttached = new TestMessage();

            DeferredChannelMessageContext outerContext    = new DeferredChannelMessageContext();
            DeferredChannelMessageContext attachedContext = new DeferredChannelMessageContext(ContextType.Attached);

            attachedContext.AddOrUpdateMessage("Attached", msgAttached, null);

            Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.True, "Attached context missing msg");
            Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.True, "Outer context missing msg");
            Assert.AreSame(msg, msgAttached);

            // Disposing inner context should not flush the messages
            attachedContext.Dispose();

            Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.True, "Attached context missing msg");
            Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.True, "Outer context missing msg");
            Assert.AreSame(msg, msgAttached);

            // Disposing outer context should flush the messages
            outerContext.Dispose();

            Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.False, "Attached context contains msg");
            Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.False, "Outer context contains msg");
        }
Beispiel #4
0
 public void Teardown()
 {
     if (_deferredChannelMessageContext != null)
     {
         _deferredChannelMessageContext.Dispose();
         _deferredChannelMessageContext = null;
     }
 }
Beispiel #5
0
        /// <summary>
        ///     Publish_s the implementation.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <param name="message">The message.</param>
        /// <param name="publishMethod">The publish method.</param>
        /// <param name="options">The options.</param>
        /// <param name="publishToOriginator">if set to <c>true</c> [publish to originator].</param>
        /// <param name="mergeAction">The merge action.</param>
        /// <returns></returns>
        private Task <long> Publish_Impl(Func <T, PublishOptions, bool, Task <long> > callback, T message, PublishMethod publishMethod, PublishOptions options = PublishOptions.None, bool publishToOriginator = false, Action <T, T> mergeAction = null)
        {
            if (Equals(message, default(T)))
            {
                return(null);
            }

            if (Suppression.IsActive( ))
            {
                return(null);
            }

            DeferredChannelMessageContext deferredMessageContext = null;

            try
            {
                /////
                // If running in a delayed context, use the current message store.
                /////
                if (publishMethod == PublishMethod.Deferred)
                {
                    deferredMessageContext = DeferredChannelMessageContext.GetContext( );

                    if (deferredMessageContext.ContextType != ContextType.Attached && !DeferredChannelMessageContext.SuppressNoContextWarning)
                    {
                        EventLog.Application.WriteWarning("Channel {0} has been created with PublishMethod.Deferred with no DeferredChannelMessageContext set. Messages will be sent immediately. Either set a DeferredChannelMessageContext or publish the message with PublishMethod.Immediate.", ChannelName);
                    }
                }

                if (deferredMessageContext != null &&
                    deferredMessageContext.ContextType == ContextType.Attached)
                {
                    deferredMessageContext.AddOrUpdateMessage(ChannelName, message, mergeAction);
                }
                else
                {
                    return(callback(message, options, publishToOriginator));
                }
            }
            finally
            {
                if (deferredMessageContext != null)
                {
                    deferredMessageContext.Dispose( );
                }
            }

            return(null);
        }