private Task HandleResponseMessage(AzureServiceBusMessage message, CancellationToken cancellationToken) { if (message.UserProperties.TryGetValue(RequestMessageIdentifierUserPropertyKey, out var requestMessageIdentifier)) { var responseMessageKey = requestMessageIdentifier?.ToString(); if (responseMessageKey.IsNullOrEmpty()) { throw new InvalidOperationException("The response message did not contain a request identifier."); } if (OutstandingResponseKeys.Contains(responseMessageKey)) { OutstandingResponseKeys.Remove(responseMessageKey); if (ResponseMessageDictionary.TryAdd(responseMessageKey, message)) { return(Task.CompletedTask); } } else { // The response is intended for another client manager. return(Task.CompletedTask); } } throw new InvalidOperationException("The response subscriber received multiple responses or the system state is corrupt."); }
internal async Task <AzureServiceBusMessage> WaitForResponseAsync(Guid requestMessageIdentifier, TimeSpan timeoutThreshold) { var responseMessageKey = requestMessageIdentifier.ToSerializedString(); var stopwatch = new Stopwatch(); if (timeoutThreshold.RejectIf(argument => argument <= TimeSpan.Zero && argument != Timeout.InfiniteTimeSpan, nameof(timeoutThreshold)) != Timeout.InfiniteTimeSpan) { stopwatch.Start(); } while (stopwatch.Elapsed < timeoutThreshold) { await Task.Delay(ResponseMessagePollingInterval).ConfigureAwait(false); if (ResponseMessageDictionary.TryRemove(responseMessageKey, out var responseMessage)) { return(responseMessage); } } throw new TimeoutException($"The timeout threshold duration was exceeded while waiting for a response to request message {responseMessageKey}."); }