public async Task <Message> ReceiveReplyAsync(TimeoutHelper timeoutHelper) { bool throwing = true; try { bool expired = false; if (!_completed) { bool wait = false; lock (ThisLock) { if (!_completed) { wait = true; _tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); } } if (wait) { expired = !await TaskHelpers.AwaitWithTimeout(_tcs.Task, timeoutHelper.RemainingTime()); lock (ThisLock) { if (!_completed) { _completed = true; } else { expired = false; } } } } if (_aborted) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_parent.CreateClosedException()); } else if (_faulted) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_parent.GetTerminalException()); } else if (expired) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.TimeoutOnRequest, _originalTimeout))); } else { throwing = false; return(_reply); } } finally { if (throwing) { WsrmFault fault = SequenceTerminatedFault.CreateCommunicationFault(_parent.session.InputID, SR.SequenceTerminatedReliableRequestThrew, null); _parent.session.OnLocalFault(null, fault, null); } } }