public void Start() { IAsyncResult asyncResult = innerChannel.BeginTryReceive(this.timeoutHelper.RemainingTime(), innerTryReceiveCompletedCallback, this); if (!asyncResult.CompletedSynchronously) { return; } bool innerReceiveCompleted = innerChannel.EndTryReceive(asyncResult, out this.message); if (!innerReceiveCompleted) { receiveCompleted = false; } else { receiveCompleted = true; bool completedSynchronously = this.OnInnerReceiveDone(ref this.message, this.timeoutHelper.RemainingTime()); if (!completedSynchronously) { return; } } Complete(true); }
private void ReceiveTryMessages(TimeSpan channelAcceptTimeout, TimeSpan messageReceiveTimeout) { IChannelListener <IInputChannel> listener = Util.GetBinding().BuildChannelListener <IInputChannel>(this.endpoint, new BindingParameterCollection()); listener.Open(); IInputChannel inputChannel = listener.AcceptChannel(channelAcceptTimeout); IAsyncResult channelResult = inputChannel.BeginOpen(channelAcceptTimeout, null, null); Thread.Sleep(TimeSpan.FromMilliseconds(50.0)); inputChannel.EndOpen(channelResult); IAsyncResult[] resultArray = new IAsyncResult[MessageCount]; for (int i = 0; i < MessageCount; i++) { resultArray[i] = inputChannel.BeginTryReceive(messageReceiveTimeout, null, null); } for (int j = 0; j < MessageCount; j++) { Message tempMessage; Assert.True(inputChannel.EndTryReceive(resultArray[j], out tempMessage), "Did not successfully receive message #{0}", j); } inputChannel.Close(); listener.Close(); }
public TryReceiveAsyncResult(IInputChannel innerChannel, TimeSpan timeout, AsyncCallback onReceiveDone, AsyncCallback callback, object state) : base(onReceiveDone, callback, state, null) { this.InnerChannel = innerChannel; OriginalResult = innerChannel.BeginTryReceive(timeout, OnComplete, this); if (OriginalResult.CompletedSynchronously) { Message message = null; this.Result = innerChannel.EndTryReceive(OriginalResult, out message); this.Message = message; this.Complete(true); } }
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; }
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); }