Skip to content

Commit f29ab5c

Browse files
(GH-672) Add ConfigureAwait when awaiting a task
1 parent 5bcb532 commit f29ab5c

File tree

6 files changed

+536
-261
lines changed

6 files changed

+536
-261
lines changed

src/DotNetToolkit.Repository.AzureStorageBlob/Internal/AzureStorageBlobRepositoryContext.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ protected async Task<Tuple<bool, object>> TryGeneratePrimaryKeyAsync<TEntity>(TE
118118
var key = await AsAsyncEnumerable<TEntity>()
119119
.Select(func)
120120
.OrderByDescending(x => x)
121-
.FirstOrDefaultAsync();
121+
.FirstOrDefaultAsync()
122+
.ConfigureAwait(false);
122123

123124
newKey = Convert.ToInt32(key) + 1;
124125
}
@@ -195,7 +196,7 @@ private async Task<BlobContainerClient> GetBlobContainerAsync<TEntity>()
195196
var blobContainer = Client.GetBlobContainerClient(container);
196197

197198
if (_createContainerIfNotExists)
198-
await blobContainer.CreateIfNotExistsAsync();
199+
await blobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
199200

200201
return blobContainer;
201202
}
@@ -204,7 +205,7 @@ private async Task<BlobClient> GetBlobClientAsync<TEntity>(TEntity entity) where
204205
{
205206
var keyValues = Conventions.GetPrimaryKeyValues(entity);
206207
var key = string.Join(":", keyValues);
207-
var blobContainer = await GetBlobContainerAsync<TEntity>();
208+
var blobContainer = await GetBlobContainerAsync<TEntity>().ConfigureAwait(false);
208209

209210
return blobContainer.GetBlobClient(key);
210211
}
@@ -214,7 +215,7 @@ private async Task<TEntity> DownloadEntityAsync<TEntity>(BlobContainerClient blo
214215
try
215216
{
216217
var blob = blobContainer.GetBlobClient(blobName);
217-
var contentResult = await blob.DownloadContentAsync(cancellationToken);
218+
var contentResult = await blob.DownloadContentAsync(cancellationToken).ConfigureAwait(false);
218219
var contentStream = contentResult.Value.Content.ToStream();
219220
var result = Deserialize<TEntity>(contentStream);
220221

@@ -228,10 +229,10 @@ private async Task<TEntity> DownloadEntityAsync<TEntity>(BlobContainerClient blo
228229

229230
private async IAsyncEnumerable<TEntity> DownloadEntitiesAsync<TEntity>() where TEntity : class
230231
{
231-
var blobContainer = await GetBlobContainerAsync<TEntity>();
232-
await foreach (var blobItem in blobContainer.GetBlobsAsync())
232+
var blobContainer = await GetBlobContainerAsync<TEntity>().ConfigureAwait(false);
233+
await foreach (var blobItem in blobContainer.GetBlobsAsync().ConfigureAwait(false))
233234
{
234-
yield return await DownloadEntityAsync<TEntity>(blobContainer, blobItem.Name);
235+
yield return await DownloadEntityAsync<TEntity>(blobContainer, blobItem.Name).ConfigureAwait(false);
235236
}
236237
}
237238

@@ -251,15 +252,15 @@ private void UploadEntity<TEntity>(TEntity entity) where TEntity : class
251252

252253
private async Task UploadEntityAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
253254
{
254-
var blob = await GetBlobClientAsync(entity);
255+
var blob = await GetBlobClientAsync(entity).ConfigureAwait(false);
255256

256-
await TryGeneratePrimaryKeyAsync(entity);
257+
await TryGeneratePrimaryKeyAsync(entity).ConfigureAwait(false);
257258

258259
using (var stream = Serialize<TEntity>(entity))
259260
{
260-
var binaryData = await BinaryData.FromStreamAsync(stream, cancellationToken);
261+
var binaryData = await BinaryData.FromStreamAsync(stream, cancellationToken).ConfigureAwait(false);
261262

262-
await blob.UploadAsync(binaryData, overwrite: true, cancellationToken);
263+
await blob.UploadAsync(binaryData, overwrite: true, cancellationToken).ConfigureAwait(false);
263264
}
264265
}
265266

@@ -354,8 +355,8 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
354355
Guard.NotEmpty(keyValues, nameof(keyValues));
355356

356357
var key = string.Join(":", keyValues);
357-
var blobContainer = await GetBlobContainerAsync<TEntity>();
358-
var result = await DownloadEntityAsync<TEntity>(blobContainer, key, cancellationToken);
358+
var blobContainer = await GetBlobContainerAsync<TEntity>().ConfigureAwait(false);
359+
var result = await DownloadEntityAsync<TEntity>(blobContainer, key, cancellationToken).ConfigureAwait(false);
359360

360361
return result;
361362
}
@@ -371,7 +372,8 @@ public async Task<TResult> FindAsync<TEntity, TResult>(IQueryOptions<TEntity> op
371372
.ApplySortingOptions(Conventions, options)
372373
.ApplyPagingOptions(options)
373374
.Select(selectorFunc)
374-
.FirstOrDefaultAsync(cancellationToken);
375+
.FirstOrDefaultAsync(cancellationToken)
376+
.ConfigureAwait(false);
375377

376378
return result;
377379
}
@@ -386,12 +388,13 @@ public async Task<PagedQueryResult<IEnumerable<TResult>>> FindAllAsync<TEntity,
386388
.ApplySpecificationOptions(options)
387389
.ApplySortingOptions(Conventions, options);
388390

389-
var total = await query.CountAsync(cancellationToken);
391+
var total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
390392

391393
var result = await query
392394
.ApplyPagingOptions(options)
393395
.Select(selectorFunc)
394-
.ToListAsync(cancellationToken);
396+
.ToListAsync(cancellationToken)
397+
.ConfigureAwait(false);
395398

396399
return new PagedQueryResult<IEnumerable<TResult>>(result, total);
397400
}
@@ -402,7 +405,8 @@ public async Task<int> CountAsync<TEntity>(IQueryOptions<TEntity> options, Cance
402405
.ApplySpecificationOptions(options)
403406
.ApplySortingOptions(Conventions, options)
404407
.ApplyPagingOptions(options)
405-
.CountAsync(cancellationToken);
408+
.CountAsync(cancellationToken)
409+
.ConfigureAwait(false);
406410

407411
return result;
408412
}
@@ -415,7 +419,8 @@ public async Task<bool> ExistsAsync<TEntity>(IQueryOptions<TEntity> options, Can
415419
.ApplySpecificationOptions(options)
416420
.ApplySortingOptions(Conventions, options)
417421
.ApplyPagingOptions(options)
418-
.AnyAsync(cancellationToken);
422+
.AnyAsync(cancellationToken)
423+
.ConfigureAwait(false);
419424

420425
return result;
421426
}
@@ -437,16 +442,17 @@ public async Task<PagedQueryResult<Dictionary<TDictionaryKey, TElement>>> ToDict
437442

438443
if (options != null && options.PageSize != -1)
439444
{
440-
total = await query.CountAsync(cancellationToken);
445+
total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
441446

442447
result = await query
443448
.ApplyPagingOptions(options)
444-
.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken);
449+
.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken)
450+
.ConfigureAwait(false);
445451
}
446452
else
447453
{
448454
// Gets the total count from memory
449-
result = await query.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken);
455+
result = await query.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken).ConfigureAwait(false);
450456
total = result.Count;
451457
}
452458

src/DotNetToolkit.Repository.EntityFramework/Internal/EfRepositoryContext.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
213213
{
214214
var keyValues = Conventions.GetPrimaryKeyValues(entity);
215215

216-
var entityInDb = await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues);
216+
var entityInDb = await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues).ConfigureAwait(false);
217217

218218
if (entityInDb != null)
219219
{
@@ -234,7 +234,7 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
234234
{
235235
var keyValues = Conventions.GetPrimaryKeyValues(entity);
236236

237-
var entityInDb = await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues);
237+
var entityInDb = await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues).ConfigureAwait(false);
238238

239239
if (entityInDb != null)
240240
{
@@ -247,9 +247,9 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
247247
}
248248
}
249249

250-
public Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
250+
public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
251251
{
252-
return _context.SaveChangesAsync(cancellationToken);
252+
return await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
253253
}
254254

255255
public async Task<IEnumerable<TEntity>> ExecuteSqlQueryAsync<TEntity>(string sql, CommandType cmdType, Dictionary<string, object> parameters, Func<IDataReader, TEntity> projector, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
@@ -262,18 +262,18 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
262262
var shouldOpenConnection = connection.State != ConnectionState.Open;
263263

264264
if (shouldOpenConnection)
265-
await connection.OpenAsync(cancellationToken);
265+
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
266266

267267
command.CommandText = sql;
268268
command.CommandType = cmdType;
269269
command.Parameters.Clear();
270270
command.AddParameters(parameters);
271271

272-
using (var reader = await command.ExecuteReaderAsync(shouldOpenConnection ? CommandBehavior.CloseConnection : CommandBehavior.Default, cancellationToken))
272+
using (var reader = await command.ExecuteReaderAsync(shouldOpenConnection ? CommandBehavior.CloseConnection : CommandBehavior.Default, cancellationToken).ConfigureAwait(false))
273273
{
274274
var list = new List<TEntity>();
275275

276-
while (await reader.ReadAsync(cancellationToken))
276+
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
277277
{
278278
list.Add(projector(reader));
279279
}
@@ -294,14 +294,14 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
294294
using (var command = connection.CreateCommand())
295295
{
296296
if (shouldOpenConnection)
297-
await connection.OpenAsync(cancellationToken);
297+
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
298298

299299
command.CommandText = sql;
300300
command.CommandType = cmdType;
301301
command.Parameters.Clear();
302302
command.AddParameters(parameters);
303303

304-
return await command.ExecuteNonQueryAsync(cancellationToken);
304+
return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
305305
}
306306
}
307307
finally
@@ -318,7 +318,7 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
318318

319319
if (fetchStrategy == null)
320320
{
321-
return await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues);
321+
return await _context.Set<TEntity>().FindAsync(cancellationToken, keyValues).ConfigureAwait(false);
322322
}
323323

324324
var options = new QueryOptions<TEntity>()
@@ -328,21 +328,23 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
328328

329329
var result = await query
330330
.ApplySpecificationOptions(options)
331-
.FirstOrDefaultAsync(cancellationToken);
331+
.FirstOrDefaultAsync(cancellationToken)
332+
.ConfigureAwait(false);
332333

333334
return result;
334335
}
335336

336-
public Task<TResult> FindAsync<TEntity, TResult>(IQueryOptions<TEntity> options, Expression<Func<TEntity, TResult>> selector, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
337+
public async Task<TResult> FindAsync<TEntity, TResult>(IQueryOptions<TEntity> options, Expression<Func<TEntity, TResult>> selector, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
337338
{
338339
Guard.NotNull(selector, nameof(selector));
339340

340-
var result = AsQueryable(options?.FetchStrategy)
341+
var result = await AsQueryable(options?.FetchStrategy)
341342
.ApplySpecificationOptions(options)
342343
.ApplySortingOptions(Conventions, options)
343344
.ApplyPagingOptions(options)
344345
.Select(selector)
345-
.FirstOrDefaultAsync(cancellationToken);
346+
.FirstOrDefaultAsync(cancellationToken)
347+
.ConfigureAwait(false);
346348

347349
return result;
348350
}
@@ -355,12 +357,13 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
355357
.ApplySpecificationOptions(options)
356358
.ApplySortingOptions(Conventions, options);
357359

358-
var total = await query.CountAsync(cancellationToken);
360+
var total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
359361

360362
var result = await query
361363
.ApplyPagingOptions(options)
362364
.Select(selector)
363-
.ToListAsync(cancellationToken);
365+
.ToListAsync(cancellationToken)
366+
.ConfigureAwait(false);
364367

365368
return new PagedQueryResult<IEnumerable<TResult>>(result, total);
366369
}
@@ -376,15 +379,16 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
376379
return result;
377380
}
378381

379-
public Task<bool> ExistsAsync<TEntity>(IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
382+
public async Task<bool> ExistsAsync<TEntity>(IQueryOptions<TEntity> options, CancellationToken cancellationToken = new CancellationToken()) where TEntity : class
380383
{
381384
Guard.NotNull(options, nameof(options));
382385

383-
var result = AsQueryable(options?.FetchStrategy)
386+
var result = await AsQueryable(options?.FetchStrategy)
384387
.ApplySpecificationOptions(options)
385388
.ApplySortingOptions(Conventions, options)
386389
.ApplyPagingOptions(options)
387-
.AnyAsync(cancellationToken);
390+
.AnyAsync(cancellationToken)
391+
.ConfigureAwait(false);
388392

389393
return result;
390394
}
@@ -406,16 +410,17 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
406410

407411
if (options != null && options.PageSize != -1)
408412
{
409-
total = await query.CountAsync(cancellationToken);
413+
total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
410414

411415
result = await query
412416
.ApplyPagingOptions(options)
413-
.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken);
417+
.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken)
418+
.ConfigureAwait(false);
414419
}
415420
else
416421
{
417422
// Gets the total count from memory
418-
result = await query.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken);
423+
result = await query.ToDictionaryAsync(keySelectFunc, elementSelectorFunc, cancellationToken).ConfigureAwait(false);
419424
total = result.Count;
420425
}
421426

@@ -435,14 +440,15 @@ public async Task<TEntity> FindAsync<TEntity>(CancellationToken cancellationToke
435440
throw new InvalidOperationException(Resources.GroupBySortingNotSupported);
436441
}
437442

438-
var total = await query.CountAsync(cancellationToken);
443+
var total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
439444

440445
var result = await query
441446
.ApplyPagingOptions(options)
442447
.GroupBy(keySelector)
443448
.OrderBy(x => x.Key)
444449
.Select(resultSelector)
445-
.ToListAsync(cancellationToken);
450+
.ToListAsync(cancellationToken)
451+
.ConfigureAwait(false);
446452

447453
return new PagedQueryResult<IEnumerable<TResult>>(result, total);
448454
}

0 commit comments

Comments
 (0)