Skip to content

Commit 947714b

Browse files
sw-joelmutTracy Boehrer
authored andcommitted
Update TimeoutException message (#6773)
1 parent 635ddce commit 947714b

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingConnection.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ protected StreamingConnection(ILogger logger)
4343
/// <value>A <see cref="ILogger"/> for the streaming connection.</value>
4444
protected ILogger Logger { get; }
4545

46+
/// <summary>
47+
/// Gets a value indicating whether this is currently connected.
48+
/// </summary>
49+
/// <value>
50+
/// True if this is currently connected, otherwise false.
51+
/// </value>
52+
protected bool IsConnected { get; private set; } = false;
53+
4654
/// <summary>
4755
/// Sends a streaming request through the connection.
4856
/// </summary>
@@ -65,7 +73,20 @@ public virtual async Task<ReceiveResponse> SendStreamingRequestAsync(StreamingRe
6573
throw new InvalidOperationException("Cannot send streaming request since the session is not set up.");
6674
}
6775

68-
return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
76+
try
77+
{
78+
return await _session.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
79+
}
80+
catch (TimeoutException ex)
81+
{
82+
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.";
83+
if (IsConnected)
84+
{
85+
timeoutMessage = $"The request sent to the client has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
86+
}
87+
88+
throw new OperationCanceledException(timeoutMessage, ex, cancellationToken);
89+
}
6990
}
7091

7192
/// <summary>
@@ -96,7 +117,7 @@ public virtual async Task ListenAsync(RequestHandler requestHandler, Cancellatio
96117
_session = new StreamingSession(requestHandler, _application, Logger, cancellationToken);
97118

98119
// Start transport and application
99-
var transportTask = _transport.ConnectAsync(default, cancellationToken);
120+
var transportTask = _transport.ConnectAsync((connected) => IsConnected = connected, cancellationToken);
100121
var applicationTask = _application.ListenAsync(cancellationToken);
101122

102123
var tasks = new List<Task> { transportTask, applicationTask };

libraries/Microsoft.Bot.Connector.Streaming/Application/StreamingTransportClient.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@ public async Task<ReceiveResponse> SendAsync(StreamingRequest message, Cancellat
105105
throw new ArgumentNullException(nameof(message));
106106
}
107107

108-
return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false);
108+
try
109+
{
110+
return await _session.SendRequestAsync(message, cancellationToken).ConfigureAwait(false);
111+
}
112+
catch (TimeoutException ex)
113+
{
114+
var timeoutMessage = $"The underlying connection has been disconnected, and the request has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
115+
if (IsConnected)
116+
{
117+
timeoutMessage = $"The request sent to the underlying connection has timed out after waiting {TaskExtensions.DefaultTimeout.Seconds} seconds for a response.";
118+
}
119+
120+
throw new OperationCanceledException(timeoutMessage, ex, cancellationToken);
121+
}
109122
}
110123

111124
/// <inheritdoc />

libraries/Microsoft.Bot.Connector.Streaming/TaskExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
using System.Threading.Tasks;
77

88
namespace Microsoft.Bot.Connector.Streaming
9-
{
9+
{
1010
internal static class TaskExtensions
1111
{
12-
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
12+
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
1313

1414
public static async Task<T> DefaultTimeOutAsync<T>(this Task<T> task)
1515
{
16-
return await task.TimeoutAfterAsync<T>(_defaultTimeout).ConfigureAwait(false);
16+
return await task.TimeoutAfterAsync<T>(DefaultTimeout).ConfigureAwait(false);
1717
}
1818

1919
public static async Task<T> TimeoutAfterAsync<T>(this Task<T> task, TimeSpan timeout)
@@ -41,7 +41,7 @@ public static async Task<T> TimeoutAfterAsync<T>(this Task<T> task, TimeSpan tim
4141

4242
public static async Task DefaultTimeOutAsync(this Task task)
4343
{
44-
await task.TimeoutAfterAsync(_defaultTimeout).ConfigureAwait(false);
44+
await task.TimeoutAfterAsync(DefaultTimeout).ConfigureAwait(false);
4545
}
4646

4747
public static async Task TimeoutAfterAsync(this Task task, TimeSpan timeout)

0 commit comments

Comments
 (0)