Example #1
0
        private void ReceiveNonTryMessages(TimeSpan channelTimeout, TimeSpan messageTimeout)
        {
            IChannelListener inputChannelParentListener;
            IInputChannel    inputChannel = this.RetrieveAsyncChannel(this.endpoint, channelTimeout, out inputChannelParentListener);

            inputChannel.Open();

            IAsyncResult[] resultArray = new IAsyncResult[MessageCount];
            try
            {
                for (int i = 0; i < MessageCount; i++)
                {
                    resultArray[i] = inputChannel.BeginReceive(messageTimeout, null, null);
                }

                for (int j = 0; j < MessageCount; j++)
                {
                    inputChannel.EndReceive(resultArray[j]);
                }
            }
            finally
            {
                IAsyncResult channelCloseResult = inputChannel.BeginClose(channelTimeout, null, null);
                Thread.Sleep(TimeSpan.FromMilliseconds(50.0)); // Dummy work
                inputChannel.EndClose(channelCloseResult);

                // Asynchronous listener close has not been implemented.
                ////IAsyncResult listenerCloseResult = inputChannelParentListener.BeginClose(channelTimeout, null, null);
                ////Thread.Sleep(TimeSpan.FromMilliseconds(50.0)); // Dummy work
                ////inputChannelParentListener.EndClose(listenerCloseResult);

                inputChannelParentListener.Close();
            }
        }
Example #2
0
        public ReceiveAsyncResult(IInputChannel innerChannel, TimeSpan timeout, AsyncCallback onReceiveDone, AsyncCallback callback, object state)
            : base(onReceiveDone, callback, state, null)
        {
            this.InnerChannel = innerChannel;

            OriginalResult = innerChannel.BeginReceive(timeout, OnComplete, this);
            if (OriginalResult.CompletedSynchronously)
            {
                this.Message = innerChannel.EndReceive(OriginalResult);
                this.Complete(true);
            }
        }
Example #3
0
        private Message ReceiveMessage(IInputChannel channel, bool commit)
        {
            Message message = null;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                bool messageDetected = false;
                if (this.Parameters.AsyncWaitForMessage)
                {
                    IAsyncResult result = channel.BeginWaitForMessage(this.Parameters.WaitForMessageTimeout, null, null);
                    messageDetected = channel.EndWaitForMessage(result);
                }
                else
                {
                    messageDetected = channel.WaitForMessage(this.Parameters.WaitForMessageTimeout);
                }

                if (this.Parameters.WaitForMessage)
                {
                    lock (this.Results)
                    {
                        this.Results.Add(String.Format("WaitForMessage returned {0}", messageDetected));
                    }
                }

                if (messageDetected)
                {
                    if (this.Parameters.AsyncReceive)
                    {
                        if (this.Parameters.TryReceive)
                        {
                            IAsyncResult result = channel.BeginTryReceive(this.Parameters.ReceiveTimeout, null, null);
                            bool ret = channel.EndTryReceive(result, out message);

                            lock (this.Results)
                            {
                                this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            }
                        }
                        else
                        {
                            try
                            {
                                IAsyncResult result = channel.BeginReceive(this.Parameters.ReceiveTimeout, null, null);
                                message = channel.EndReceive(result);
                            }
                            catch (TimeoutException)
                            {
                                message = null;
                            }
                        }
                    }
                    else
                    {
                        if (this.Parameters.TryReceive)
                        {
                            bool ret = channel.TryReceive(this.Parameters.ReceiveTimeout, out message);

                            lock (this.Results)
                            {
                                this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            }
                        }
                        else
                        {
                            try
                            {
                                message = channel.Receive(this.Parameters.ReceiveTimeout);
                            }
                            catch (TimeoutException)
                            {
                                message = null;
                            }
                        }
                    }
                }
                else
                {
                    if (this.Parameters.TryReceive)
                    {
                        bool ret = false;
                        if (this.Parameters.AsyncReceive)
                        {
                            IAsyncResult result = channel.BeginTryReceive(this.Parameters.ReceiveTimeout, null, null);
                            if (this.Parameters.TryReceiveNullIAsyncResult)
                            {
                                try
                                {
                                    channel.EndTryReceive(null, out message);
                                }
                                catch (Exception e)
                                {
                                    lock (this.Results)
                                    {
                                        this.Results.Add(String.Format("TryReceive threw {0}", e.GetType().Name));
                                    }
                                }
                            }

                            ret = channel.EndTryReceive(result, out message);
                        }
                        else
                        {
                            ret = channel.TryReceive(this.Parameters.ReceiveTimeout, out message);
                        }

                        lock (this.Results)
                        {
                            this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            this.Results.Add(String.Format("Message was {0}", (message == null ? "null" : "not null")));
                        }
                    }

                    message = null;
                }

                if (commit && message != null)
                {
                    lock (this.Results)
                    {
                        this.Results.Add(String.Format("Received message with Action '{0}'", message.Headers.Action));
                    }

                    ts.Complete();
                }
                else
                {
                    Transaction.Current.Rollback();
                }
            }

            return message;
        }
Example #4
0
        private Message ReceiveMessage(IInputChannel channel, bool commit)
        {
            Message message = null;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                bool messageDetected = false;
                if (this.Parameters.AsyncWaitForMessage)
                {
                    IAsyncResult result = channel.BeginWaitForMessage(this.Parameters.WaitForMessageTimeout, null, null);
                    messageDetected = channel.EndWaitForMessage(result);
                }
                else
                {
                    messageDetected = channel.WaitForMessage(this.Parameters.WaitForMessageTimeout);
                }

                if (this.Parameters.WaitForMessage)
                {
                    lock (this.Results)
                    {
                        this.Results.Add(String.Format("WaitForMessage returned {0}", messageDetected));
                    }
                }

                if (messageDetected)
                {
                    if (this.Parameters.AsyncReceive)
                    {
                        if (this.Parameters.TryReceive)
                        {
                            IAsyncResult result = channel.BeginTryReceive(this.Parameters.ReceiveTimeout, null, null);
                            bool         ret    = channel.EndTryReceive(result, out message);

                            lock (this.Results)
                            {
                                this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            }
                        }
                        else
                        {
                            try
                            {
                                IAsyncResult result = channel.BeginReceive(this.Parameters.ReceiveTimeout, null, null);
                                message = channel.EndReceive(result);
                            }
                            catch (TimeoutException)
                            {
                                message = null;
                            }
                        }
                    }
                    else
                    {
                        if (this.Parameters.TryReceive)
                        {
                            bool ret = channel.TryReceive(this.Parameters.ReceiveTimeout, out message);

                            lock (this.Results)
                            {
                                this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            }
                        }
                        else
                        {
                            try
                            {
                                message = channel.Receive(this.Parameters.ReceiveTimeout);
                            }
                            catch (TimeoutException)
                            {
                                message = null;
                            }
                        }
                    }
                }
                else
                {
                    if (this.Parameters.TryReceive)
                    {
                        bool ret = false;
                        if (this.Parameters.AsyncReceive)
                        {
                            IAsyncResult result = channel.BeginTryReceive(this.Parameters.ReceiveTimeout, null, null);
                            if (this.Parameters.TryReceiveNullIAsyncResult)
                            {
                                try
                                {
                                    channel.EndTryReceive(null, out message);
                                }
                                catch (Exception e)
                                {
                                    lock (this.Results)
                                    {
                                        this.Results.Add(String.Format("TryReceive threw {0}", e.GetType().Name));
                                    }
                                }
                            }

                            ret = channel.EndTryReceive(result, out message);
                        }
                        else
                        {
                            ret = channel.TryReceive(this.Parameters.ReceiveTimeout, out message);
                        }

                        lock (this.Results)
                        {
                            this.Results.Add(String.Format("TryReceive returned {0}", ret));
                            this.Results.Add(String.Format("Message was {0}", (message == null ? "null" : "not null")));
                        }
                    }

                    message = null;
                }

                if (commit && message != null)
                {
                    lock (this.Results)
                    {
                        this.Results.Add(String.Format("Received message with Action '{0}'", message.Headers.Action));
                    }

                    ts.Complete();
                }
                else
                {
                    Transaction.Current.Rollback();
                }
            }

            return(message);
        }