Skip to content

Commit 864e88d

Browse files
committed
.ConfigureAwait(false) in SignalR client libraries
1 parent 9c3ff54 commit 864e88d

22 files changed

+157
-133
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.{cs,vb}]
2+
3+
# CA2007: Consider calling ConfigureAwait on the awaited task
4+
dotnet_diagnostic.CA2007.severity = warning

src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs

Lines changed: 63 additions & 63 deletions
Large diffs are not rendered by default.

src/SignalR/clients/csharp/Client.Core/src/HubConnectionExtensions.InvokeAsyncGeneric.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static async Task<TResult> InvokeCoreAsync<TResult>(this HubConnection hu
274274
throw new ArgumentNullException(nameof(hubConnection));
275275
}
276276

277-
return (TResult)(await hubConnection.InvokeCoreAsync(methodName, typeof(TResult), args, cancellationToken))!;
277+
return (TResult)(await hubConnection.InvokeCoreAsync(methodName, typeof(TResult), args, cancellationToken).ConfigureAwait(false))!;
278278
}
279279

280280
}

src/SignalR/clients/csharp/Client.Core/src/HubConnectionExtensions.StreamAsChannelAsync.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static async Task<ChannelReader<TResult>> StreamAsChannelCoreAsync<TResul
275275
throw new ArgumentNullException(nameof(hubConnection));
276276
}
277277

278-
var inputChannel = await hubConnection.StreamAsChannelCoreAsync(methodName, typeof(TResult), args, cancellationToken);
278+
var inputChannel = await hubConnection.StreamAsChannelCoreAsync(methodName, typeof(TResult), args, cancellationToken).ConfigureAwait(false);
279279
var outputChannel = Channel.CreateUnbounded<TResult>();
280280

281281
// Intentionally avoid passing the CancellationToken to RunChannel. The token is only meant to cancel the intial setup, not the enumeration.
@@ -290,13 +290,13 @@ private static async Task RunChannel<TResult>(ChannelReader<object?> inputChanne
290290
{
291291
try
292292
{
293-
while (await inputChannel.WaitToReadAsync())
293+
while (await inputChannel.WaitToReadAsync().ConfigureAwait(false))
294294
{
295295
while (inputChannel.TryRead(out var item))
296296
{
297297
while (!outputChannel.Writer.TryWrite((TResult)item!))
298298
{
299-
if (!await outputChannel.Writer.WaitToWriteAsync())
299+
if (!await outputChannel.Writer.WaitToWriteAsync().ConfigureAwait(false))
300300
{
301301
// Failed to write to the output channel because it was closed. Nothing really we can do but abort here.
302302
return;
@@ -306,7 +306,7 @@ private static async Task RunChannel<TResult>(ChannelReader<object?> inputChanne
306306
}
307307

308308
// Manifest any errors in the completion task
309-
await inputChannel.Completion;
309+
await inputChannel.Completion.ConfigureAwait(false);
310310
}
311311
catch (Exception ex)
312312
{

src/SignalR/clients/csharp/Client.Core/src/Internal/InvocationRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override async ValueTask<bool> StreamItem(object? item)
106106
{
107107
while (!_channel.Writer.TryWrite(item))
108108
{
109-
if (!await _channel.Writer.WaitToWriteAsync())
109+
if (!await _channel.Writer.WaitToWriteAsync().ConfigureAwait(false))
110110
{
111111
return false;
112112
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.{cs,vb}]
2+
3+
# CA2007: Consider calling ConfigureAwait on the awaited task
4+
dotnet_diagnostic.CA2007.severity = silent

src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public async Task StartAsync(TransferFormat transferFormat, CancellationToken ca
204204
{
205205
using (_logger.BeginScope(_logScope))
206206
{
207-
await StartAsyncCore(transferFormat, cancellationToken);
207+
await StartAsyncCore(transferFormat, cancellationToken).ConfigureAwait(false);
208208
}
209209
}
210210

@@ -218,7 +218,7 @@ private async Task StartAsyncCore(TransferFormat transferFormat, CancellationTok
218218
return;
219219
}
220220

221-
await _connectionLock.WaitAsync(cancellationToken);
221+
await _connectionLock.WaitAsync(cancellationToken).ConfigureAwait(false);
222222
try
223223
{
224224
CheckDisposed();
@@ -231,7 +231,7 @@ private async Task StartAsyncCore(TransferFormat transferFormat, CancellationTok
231231

232232
Log.Starting(_logger);
233233

234-
await SelectAndStartTransport(transferFormat, cancellationToken);
234+
await SelectAndStartTransport(transferFormat, cancellationToken).ConfigureAwait(false);
235235

236236
_started = true;
237237
Log.Started(_logger);
@@ -254,7 +254,7 @@ public override async ValueTask DisposeAsync()
254254
{
255255
using (_logger.BeginScope(_logScope))
256256
{
257-
await DisposeAsyncCore();
257+
await DisposeAsyncCore().ConfigureAwait(false);
258258
}
259259
}
260260

@@ -265,7 +265,7 @@ private async Task DisposeAsyncCore()
265265
return;
266266
}
267267

268-
await _connectionLock.WaitAsync();
268+
await _connectionLock.WaitAsync().ConfigureAwait(false);
269269
try
270270
{
271271
if (!_disposed && _started)
@@ -276,7 +276,7 @@ private async Task DisposeAsyncCore()
276276
// The transport should also have completed the pipe with this exception.
277277
try
278278
{
279-
await _transport!.StopAsync();
279+
await _transport!.StopAsync().ConfigureAwait(false);
280280
}
281281
catch (Exception ex)
282282
{
@@ -317,7 +317,7 @@ private async Task SelectAndStartTransport(TransferFormat transferFormat, Cancel
317317
if (_httpConnectionOptions.Transports == HttpTransportType.WebSockets)
318318
{
319319
Log.StartingTransport(_logger, _httpConnectionOptions.Transports, uri);
320-
await StartTransport(uri, _httpConnectionOptions.Transports, transferFormat, cancellationToken);
320+
await StartTransport(uri, _httpConnectionOptions.Transports, transferFormat, cancellationToken).ConfigureAwait(false);
321321
}
322322
else
323323
{
@@ -331,7 +331,7 @@ private async Task SelectAndStartTransport(TransferFormat transferFormat, Cancel
331331

332332
do
333333
{
334-
negotiationResponse = await GetNegotiationResponseAsync(uri, cancellationToken);
334+
negotiationResponse = await GetNegotiationResponseAsync(uri, cancellationToken).ConfigureAwait(false);
335335

336336
if (negotiationResponse.Url != null)
337337
{
@@ -402,12 +402,12 @@ private async Task SelectAndStartTransport(TransferFormat transferFormat, Cancel
402402
// The negotiation response gets cleared in the fallback scenario.
403403
if (negotiationResponse == null)
404404
{
405-
negotiationResponse = await GetNegotiationResponseAsync(uri, cancellationToken);
405+
negotiationResponse = await GetNegotiationResponseAsync(uri, cancellationToken).ConfigureAwait(false);
406406
connectUrl = CreateConnectUrl(uri, negotiationResponse.ConnectionToken);
407407
}
408408

409409
Log.StartingTransport(_logger, transportType, uri);
410-
await StartTransport(connectUrl, transportType, transferFormat, cancellationToken);
410+
await StartTransport(connectUrl, transportType, transferFormat, cancellationToken).ConfigureAwait(false);
411411
break;
412412
}
413413
}
@@ -468,11 +468,11 @@ private async Task<NegotiationResponse> NegotiateAsync(Uri url, HttpClient httpC
468468
// rather than buffer the entire response. This gives a small perf boost.
469469
// Note that it is important to dispose of the response when doing this to
470470
// avoid leaving the connection open.
471-
using (var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
471+
using (var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
472472
{
473473
response.EnsureSuccessStatusCode();
474474
#pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods
475-
var responseBuffer = await response.Content.ReadAsByteArrayAsync();
475+
var responseBuffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
476476
#pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods
477477
var negotiateResponse = NegotiateProtocol.ParseResponse(responseBuffer);
478478
if (!string.IsNullOrEmpty(negotiateResponse.Error))
@@ -509,7 +509,7 @@ private async Task StartTransport(Uri connectUrl, HttpTransportType transportTyp
509509
// Start the transport, giving it one end of the pipe
510510
try
511511
{
512-
await transport.StartAsync(connectUrl, transferFormat, cancellationToken);
512+
await transport.StartAsync(connectUrl, transferFormat, cancellationToken).ConfigureAwait(false);
513513
}
514514
catch (Exception ex)
515515
{
@@ -683,7 +683,7 @@ private static bool IsWebSocketsSupported()
683683

684684
private async Task<NegotiationResponse> GetNegotiationResponseAsync(Uri uri, CancellationToken cancellationToken)
685685
{
686-
var negotiationResponse = await NegotiateAsync(uri, _httpClient!, _logger, cancellationToken);
686+
var negotiationResponse = await NegotiateAsync(uri, _httpClient!, _logger, cancellationToken).ConfigureAwait(false);
687687
// If the negotiationVersion is greater than zero then we know that the negotiation response contains a
688688
// connectionToken that will be required to conenct. Otherwise we just set the connectionId and the
689689
// connectionToken on the client to the same value.

src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnectionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public async ValueTask<ConnectionContext> ConnectAsync(EndPoint endPoint, Cancel
6868

6969
try
7070
{
71-
await connection.StartAsync(cancellationToken);
71+
await connection.StartAsync(cancellationToken).ConfigureAwait(false);
7272
return connection;
7373
}
7474
catch
7575
{
7676
// Make sure the connection is disposed, in case it allocated any resources before failing.
77-
await connection.DisposeAsync();
77+
await connection.DisposeAsync().ConfigureAwait(false);
7878
throw;
7979
}
8080
}

src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/AccessTokenHttpMessageHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public AccessTokenHttpMessageHandler(HttpMessageHandler inner, HttpConnection ht
1919

2020
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
2121
{
22-
var accessToken = await _httpConnection.GetAccessTokenAsync();
22+
var accessToken = await _httpConnection.GetAccessTokenAsync().ConfigureAwait(false);
2323

2424
if (!string.IsNullOrEmpty(accessToken))
2525
{
2626
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
2727
}
2828

29-
return await base.SendAsync(request, cancellationToken);
29+
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
3030
}
3131
}

src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/LoggingHttpMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
2828
{
2929
Log.SendingHttpRequest(_logger, request.Method, request.RequestUri!);
3030

31-
var response = await base.SendAsync(request, cancellationToken);
31+
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
3232

3333
if (!response.IsSuccessStatusCode)
3434
{

0 commit comments

Comments
 (0)