From 947714b4da8a00c8f1da51779f1f6cc4aca5a602 Mon Sep 17 00:00:00 2001 From: Joel Mut <62260472+sw-joelmut@users.noreply.github.com> Date: Thu, 11 Apr 2024 10:17:01 -0300 Subject: [PATCH] Update TimeoutException message (#6773) --- .../Application/StreamingConnection.cs | 25 +++++++++++++++++-- .../Application/StreamingTransportClient.cs | 15 ++++++++++- .../TaskExtensions.cs | 8 +++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingConnection.cs b/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingConnection.cs index 40b3af14da..d9effa885f 100644 --- a/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingConnection.cs +++ b/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingConnection.cs @@ -43,6 +43,14 @@ protected StreamingConnection(ILogger logger) /// A for the streaming connection. protected ILogger Logger { get; } + /// + /// Gets a value indicating whether this is currently connected. + /// + /// + /// True if this is currently connected, otherwise false. + /// + protected bool IsConnected { get; private set; } = false; + /// /// Sends a streaming request through the connection. /// @@ -65,7 +73,20 @@ public virtual async Task SendStreamingRequestAsync(StreamingRe throw new InvalidOperationException("Cannot send streaming request since the session is not set up."); } - return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); + try + { + return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); + } + catch (TimeoutException ex) + { + var timeoutMessage = $"The connection to the client has been disconnected, and the request has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response."; + if (IsConnected) + { + timeoutMessage = $"The request sent to the client has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response."; + } + + throw new OperationCanceledException(timeoutMessage, ex, cancellationToken); + } } /// @@ -96,7 +117,7 @@ public virtual async Task ListenAsync(RequestHandler requestHandler, Cancellatio _session = new StreamingSession(requestHandler, _application, Logger, cancellationToken); // Start transport and application - var transportTask = _transport.ConnectAsync(default, cancellationToken); + var transportTask = _transport.ConnectAsync((connected) => IsConnected = connected, cancellationToken); var applicationTask = _application.ListenAsync(cancellationToken); var tasks = new List { transportTask, applicationTask }; diff --git a/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingTransportClient.cs b/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingTransportClient.cs index 16097d1a7a..73b8153504 100644 --- a/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingTransportClient.cs +++ b/libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingTransportClient.cs @@ -105,7 +105,20 @@ public async Task SendAsync(StreamingRequest message, Cancellat throw new ArgumentNullException(nameof(message)); } - return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false); + try + { + return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false); + } + catch (TimeoutException ex) + { + var timeoutMessage = $"The underlying connection has been disconnected, and the request has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response."; + if (IsConnected) + { + timeoutMessage = $"The request sent to the underlying connection has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response."; + } + + throw new OperationCanceledException(timeoutMessage, ex, cancellationToken); + } } /// diff --git a/libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs b/libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs index 8f39d051a8..0940a437a9 100644 --- a/libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs +++ b/libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs @@ -6,14 +6,14 @@ using System.Threading.Tasks; namespace Microsoft.Bot.Connector.Streaming -{ +{ internal static class TaskExtensions { - private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30); + public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); public static async Task DefaultTimeOutAsync(this Task task) { - return await task.TimeoutAfterAsync(_defaultTimeout).ConfigureAwait(false); + return await task.TimeoutAfterAsync(DefaultTimeout).ConfigureAwait(false); } public static async Task TimeoutAfterAsync(this Task task, TimeSpan timeout) @@ -41,7 +41,7 @@ public static async Task TimeoutAfterAsync(this Task task, TimeSpan tim public static async Task DefaultTimeOutAsync(this Task task) { - await task.TimeoutAfterAsync(_defaultTimeout).ConfigureAwait(false); + await task.TimeoutAfterAsync(DefaultTimeout).ConfigureAwait(false); } public static async Task TimeoutAfterAsync(this Task task, TimeSpan timeout)