Skip to content

Commit 79d3917

Browse files
committed
Use ConfigureAwait(false) for .NET 4.x
1 parent 16dd148 commit 79d3917

File tree

10 files changed

+137
-88
lines changed

10 files changed

+137
-88
lines changed

Http/src/AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect/OpenIdConnectOAuthClientOptionsResolver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected override async Task<OAuthClientOptions> GetOptionsFromSchemeAsync(
3131
OpenIdConnectConfiguration oidcConfig;
3232
try
3333
{
34-
oidcConfig = await oidcOptions.ConfigurationManager!.GetConfigurationAsync(default);
34+
oidcConfig = await oidcOptions.ConfigurationManager!.GetConfigurationAsync(default)
35+
.ConfigureAwait(false);
3536
}
3637
catch (Exception e)
3738
{

Http/src/AppCore.Extensions.Http.Authentication.OAuth.AspNetCore/AuthenticationSchemeOAuthClientOptionsResolver.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
6060
Microsoft.AspNetCore.Authentication.AuthenticationScheme? authenticationScheme =
6161
string.IsNullOrWhiteSpace(clientOptions.Scheme)
6262
? await _authenticationSchemeProvider.GetDefaultChallengeSchemeAsync()
63-
: await _authenticationSchemeProvider.GetSchemeAsync(clientOptions.Scheme);
63+
.ConfigureAwait(false)
64+
: await _authenticationSchemeProvider.GetSchemeAsync(clientOptions.Scheme)
65+
.ConfigureAwait(false);
6466

6567
if (authenticationScheme is null)
6668
{
@@ -76,8 +78,9 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
7678
}
7779

7880
result = (T)(object)await GetOptionsFromSchemeAsync(
79-
_clientOptions.Get(scheme.Name),
80-
_authenticationSchemeOptions.Get(authenticationScheme.Name));
81+
_clientOptions.Get(scheme.Name),
82+
_authenticationSchemeOptions.Get(authenticationScheme.Name))
83+
.ConfigureAwait(false);
8184
}
8285

8386
return result;

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthClientHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public async Task AuthenticateAsync(
3434
CancellationToken cancellationToken = default)
3535
{
3636
OAuthAccessToken accessToken =
37-
await _authTokenService.GetClientAccessTokenAsync(scheme, parameters, cancellationToken);
37+
await _authTokenService.GetClientAccessTokenAsync(scheme, parameters, cancellationToken)
38+
.ConfigureAwait(false);
3839

3940
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
4041
}

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthOptionsProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public virtual async Task<T> GetOptionsAsync<T>(AuthenticationScheme scheme)
3333
T? result = null;
3434
foreach (IOAuthOptionsResolver resolver in _resolvers)
3535
{
36-
result = await resolver.TryGetOptionsAsync<T>(scheme);
36+
result = await resolver.TryGetOptionsAsync<T>(scheme)
37+
.ConfigureAwait(false);
38+
3739
if (result != null)
3840
break;
3941
}

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthPasswordHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public async Task AuthenticateAsync(
3131
CancellationToken cancellationToken = default)
3232
{
3333
OAuthAccessToken accessToken =
34-
await _authTokenService.GetPasswordAccessTokenAsync(scheme, parameters, cancellationToken);
34+
await _authTokenService.GetPasswordAccessTokenAsync(scheme, parameters, cancellationToken)
35+
.ConfigureAwait(false);
3536

3637
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
3738
}

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthTokenCache.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ public async Task SetAsync(
9393
cacheExpiration);
9494

9595
await _cache.SetAsync(
96-
GenerateCacheKey(scheme, options, parameters),
97-
SerializeAccessToken(accessToken),
98-
new DistributedCacheEntryOptions { AbsoluteExpiration = cacheExpiration },
99-
cancellationToken);
96+
GenerateCacheKey(scheme, options, parameters),
97+
SerializeAccessToken(accessToken),
98+
new DistributedCacheEntryOptions { AbsoluteExpiration = cacheExpiration },
99+
cancellationToken)
100+
.ConfigureAwait(false);
100101
}
101102

102103
/// <inheritdoc />
@@ -110,7 +111,9 @@ await _cache.SetAsync(
110111
OAuthTokenCacheOptions options = _optionsMonitor.CurrentValue;
111112

112113
string cacheKey = GenerateCacheKey(scheme, options, parameters);
113-
OAuthAccessToken? result = DeserializeAccessToken(await _cache.GetAsync(cacheKey, cancellationToken));
114+
OAuthAccessToken? result = DeserializeAccessToken(
115+
await _cache.GetAsync(cacheKey, cancellationToken)
116+
.ConfigureAwait(false));
114117

115118
if (result != null)
116119
{

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthTokenClient.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public async Task<TokenResponse> RequestClientAccessToken(
3838
OAuthParameters? parameters = null,
3939
CancellationToken cancellationToken = default)
4040
{
41-
var options = await _optionsProvider.GetOptionsAsync<OAuthClientOptions>(scheme);
41+
OAuthClientOptions options =
42+
await _optionsProvider.GetOptionsAsync<OAuthClientOptions>(scheme)
43+
.ConfigureAwait(false);
4244

4345
var request = new ClientCredentialsTokenRequest
4446
{
@@ -54,7 +56,8 @@ public async Task<TokenResponse> RequestClientAccessToken(
5456
if (parameters != null && !string.IsNullOrWhiteSpace(parameters.Resource))
5557
request.Resource.Add(parameters.Resource);
5658

57-
return await _client.RequestClientCredentialsTokenAsync(request, cancellationToken);
59+
return await _client.RequestClientCredentialsTokenAsync(request, cancellationToken)
60+
.ConfigureAwait(false);
5861
}
5962

6063
/// <inheritdoc />
@@ -63,7 +66,9 @@ public async Task<TokenResponse> RequestPasswordAccessToken(
6366
OAuthParameters? parameters = null,
6467
CancellationToken cancellationToken = default)
6568
{
66-
var options = await _optionsProvider.GetOptionsAsync<OAuthPasswordOptions>(scheme);
69+
OAuthPasswordOptions options =
70+
await _optionsProvider.GetOptionsAsync<OAuthPasswordOptions>(scheme)
71+
.ConfigureAwait(false);
6772

6873
var request = new PasswordTokenRequest
6974
{
@@ -81,6 +86,7 @@ public async Task<TokenResponse> RequestPasswordAccessToken(
8186
if (parameters != null && !string.IsNullOrWhiteSpace(parameters.Resource))
8287
request.Resource.Add(parameters.Resource);
8388

84-
return await _client.RequestPasswordTokenAsync(request, cancellationToken);
89+
return await _client.RequestPasswordTokenAsync(request, cancellationToken)
90+
.ConfigureAwait(false);
8591
}
8692
}

Http/src/AppCore.Extensions.Http.Authentication.OAuth/OAuthTokenService.cs

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private async Task<OAuthAccessToken> InvokeSynchronized(AuthenticationScheme sch
4646
return await _sync.GetOrAdd(
4747
scheme.Name,
4848
_ => new Lazy<Task<OAuthAccessToken>>(tokenFunc))
49-
.Value;
49+
.Value.ConfigureAwait(false);
5050
}
5151
finally
5252
{
@@ -66,46 +66,55 @@ public async Task<OAuthAccessToken> GetClientAccessTokenAsync(
6666

6767
if (parameters == null || parameters.ForceRenewal == false)
6868
{
69-
OAuthAccessToken? item = await _cache.GetAsync(scheme, parameters, cancellationToken);
69+
OAuthAccessToken? item =
70+
await _cache.GetAsync(scheme, parameters, cancellationToken)
71+
.ConfigureAwait(false);
72+
7073
if (item != null)
7174
{
7275
return item;
7376
}
7477
}
7578

7679
return await InvokeSynchronized(
77-
scheme,
78-
async () =>
79-
{
80-
_logger.LogDebug("Requesting access token for client scheme {schemeName} ...", scheme.Name);
81-
82-
TokenResponse response = await _client.RequestClientAccessToken(scheme, parameters, cancellationToken);
83-
if (response.IsError)
80+
scheme,
81+
async () =>
8482
{
85-
_logger.LogError(
86-
"Error requesting access token for client scheme {schemeName}. Error = {error}. Error description = {errorDescription}",
83+
_logger.LogDebug("Requesting access token for client scheme {schemeName} ...", scheme.Name);
84+
85+
TokenResponse response =
86+
await _client.RequestClientAccessToken(scheme, parameters, cancellationToken)
87+
.ConfigureAwait(false);
88+
89+
if (response.IsError)
90+
{
91+
_logger.LogError(
92+
"Error requesting access token for client scheme {schemeName}. Error = {error}. Error description = {errorDescription}",
93+
scheme.Name,
94+
response.Error,
95+
response.ErrorDescription);
96+
97+
throw new AuthenticationException(
98+
$"Error requesting access token for client scheme {scheme.Name}");
99+
}
100+
101+
OAuthAccessToken token = new(
102+
response.AccessToken,
103+
response.ExpiresIn > 0
104+
? DateTimeOffset.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn)
105+
: null);
106+
107+
_logger.LogDebug(
108+
"Received access token for client scheme {schemeName}. Expiration: {expiration}",
87109
scheme.Name,
88-
response.Error,
89-
response.ErrorDescription);
90-
91-
throw new AuthenticationException(
92-
$"Error requesting access token for client scheme {scheme.Name}");
93-
}
94-
95-
OAuthAccessToken token = new(
96-
response.AccessToken,
97-
response.ExpiresIn > 0
98-
? DateTimeOffset.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn)
99-
: null);
100-
101-
_logger.LogDebug(
102-
"Received access token for client scheme {schemeName}. Expiration: {expiration}",
103-
scheme.Name,
104-
token.Expires);
105-
106-
await _cache.SetAsync(scheme, token, parameters, cancellationToken);
107-
return token;
108-
});
110+
token.Expires);
111+
112+
await _cache.SetAsync(scheme, token, parameters, cancellationToken)
113+
.ConfigureAwait(false);
114+
115+
return token;
116+
})
117+
.ConfigureAwait(false);
109118
}
110119

111120
/// <inheritdoc />
@@ -120,46 +129,55 @@ public async Task<OAuthAccessToken> GetPasswordAccessTokenAsync(
120129

121130
if (parameters == null || parameters.ForceRenewal == false)
122131
{
123-
OAuthAccessToken? item = await _cache.GetAsync(scheme, parameters, cancellationToken);
132+
OAuthAccessToken? item =
133+
await _cache.GetAsync(scheme, parameters, cancellationToken)
134+
.ConfigureAwait(false);
135+
124136
if (item != null)
125137
{
126138
return item;
127139
}
128140
}
129141

130142
return await InvokeSynchronized(
131-
scheme,
132-
async () =>
133-
{
134-
_logger.LogDebug("Requesting access token for password scheme {schemeName} ...", scheme.Name);
135-
136-
TokenResponse response = await _client.RequestPasswordAccessToken(scheme, parameters, cancellationToken);
137-
if (response.IsError)
143+
scheme,
144+
async () =>
138145
{
139-
_logger.LogError(
140-
"Error requesting access token for password scheme {schemeName}. Error = {error}. Error description = {errorDescription}",
146+
_logger.LogDebug("Requesting access token for password scheme {schemeName} ...", scheme.Name);
147+
148+
TokenResponse response =
149+
await _client.RequestPasswordAccessToken(scheme, parameters, cancellationToken)
150+
.ConfigureAwait(false);
151+
152+
if (response.IsError)
153+
{
154+
_logger.LogError(
155+
"Error requesting access token for password scheme {schemeName}. Error = {error}. Error description = {errorDescription}",
156+
scheme.Name,
157+
response.Error,
158+
response.ErrorDescription);
159+
160+
throw new AuthenticationException(
161+
$"Error requesting access token for password scheme {scheme.Name}");
162+
}
163+
164+
OAuthAccessToken token = new(
165+
response.AccessToken,
166+
response.ExpiresIn > 0
167+
? DateTimeOffset.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn)
168+
: null);
169+
170+
_logger.LogDebug(
171+
"Received access token for password scheme {schemeName}. Expiration: {expiration}",
141172
scheme.Name,
142-
response.Error,
143-
response.ErrorDescription);
144-
145-
throw new AuthenticationException(
146-
$"Error requesting access token for password scheme {scheme.Name}");
147-
}
148-
149-
OAuthAccessToken token = new(
150-
response.AccessToken,
151-
response.ExpiresIn > 0
152-
? DateTimeOffset.UtcNow + TimeSpan.FromSeconds(response.ExpiresIn)
153-
: null);
154-
155-
_logger.LogDebug(
156-
"Received access token for password scheme {schemeName}. Expiration: {expiration}",
157-
scheme.Name,
158-
token.Expires);
159-
160-
await _cache.SetAsync(scheme, token, parameters, cancellationToken);
161-
return token;
162-
});
173+
token.Expires);
174+
175+
await _cache.SetAsync(scheme, token, parameters, cancellationToken)
176+
.ConfigureAwait(false);
177+
178+
return token;
179+
})
180+
.ConfigureAwait(false);
163181
}
164182

165183
/// <inheritdoc />
@@ -169,6 +187,8 @@ public async Task DeleteAccessTokenAsync(
169187
CancellationToken cancellationToken = default)
170188
{
171189
Ensure.Arg.NotNull(scheme);
172-
await _cache.DeleteAsync(scheme, parameters, cancellationToken);
190+
191+
await _cache.DeleteAsync(scheme, parameters, cancellationToken)
192+
.ConfigureAwait(false);
173193
}
174194
}

Http/src/AppCore.Extensions.Http.Authentication/AuthenticationHandler.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public AuthenticationHandler(
5656

5757
private async Task<AuthenticationScheme> GetSchemeAsync()
5858
{
59-
AuthenticationScheme? authenticationScheme = await _schemes.FindSchemeAsync(_scheme);
59+
AuthenticationScheme? authenticationScheme =
60+
await _schemes.FindSchemeAsync(_scheme)
61+
.ConfigureAwait(false);
62+
6063
if (authenticationScheme == null)
6164
{
6265
throw new InvalidOperationException($"There is no client authentication scheme registered with name {_scheme}.");
@@ -76,12 +79,16 @@ protected override async Task<HttpResponseMessage> SendAsync(
7679
HttpRequestMessage request,
7780
CancellationToken cancellationToken)
7881
{
79-
AuthenticationScheme scheme = await GetSchemeAsync();
82+
AuthenticationScheme scheme = await GetSchemeAsync()
83+
.ConfigureAwait(false);
8084

8185
_logger.LogDebug("Authenticating HTTP request with scheme {schemeName}.", scheme.Name);
8286

83-
await AuthenticateAsync(scheme, request, forceRenewal: false, cancellationToken);
84-
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
87+
await AuthenticateAsync(scheme, request, forceRenewal: false, cancellationToken)
88+
.ConfigureAwait(false);
89+
90+
HttpResponseMessage response = await base.SendAsync(request, cancellationToken)
91+
.ConfigureAwait(false);
8592

8693
// retry if 401
8794
if (response.StatusCode == HttpStatusCode.Unauthorized)
@@ -90,8 +97,11 @@ protected override async Task<HttpResponseMessage> SendAsync(
9097

9198
response.Dispose();
9299

93-
await AuthenticateAsync(scheme, request, forceRenewal: true, cancellationToken);
94-
return await base.SendAsync(request, cancellationToken);
100+
await AuthenticateAsync(scheme, request, forceRenewal: true, cancellationToken)
101+
.ConfigureAwait(false);
102+
103+
return await base.SendAsync(request, cancellationToken)
104+
.ConfigureAwait(false);
95105
}
96106

97107
return response;
@@ -126,6 +136,7 @@ protected virtual async Task AuthenticateAsync(
126136
parameters.ForceRenewal = forceRenewal;
127137
}
128138

129-
await _schemeHandler.AuthenticateAsync(scheme, parameters, request, cancellationToken);
139+
await _schemeHandler.AuthenticateAsync(scheme, parameters, request, cancellationToken)
140+
.ConfigureAwait(false);
130141
}
131142
}

Http/src/AppCore.Extensions.Http.Authentication/AuthenticationSchemeHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async Task IAuthenticationSchemeHandler<TParameters>.AuthenticateAsync(
5252
HttpRequestMessage request,
5353
CancellationToken cancellationToken)
5454
{
55-
await AuthenticateAsync(scheme, _optionsMonitor.Get(scheme.Name), parameters, request, cancellationToken);
55+
await AuthenticateAsync(scheme, _optionsMonitor.Get(scheme.Name), parameters, request, cancellationToken)
56+
.ConfigureAwait(false);
5657
}
5758
}

0 commit comments

Comments
 (0)