1
0

Optimizing async tasks (ConfigureAwait(false))

This commit is contained in:
2023-11-03 10:12:30 +01:00
parent 18b9627ea4
commit ca76966827
11 changed files with 61 additions and 43 deletions

View File

@@ -45,21 +45,21 @@ namespace Microsoft.EntityFrameworkCore
{
opts.WaitDelay = options.WaitDelay;
opts.Logger = options.Logger;
}, cancellationToken);
}, cancellationToken).ConfigureAwait(false);
var connection = database.GetDbConnection();
try
{
await connection.OpenAsync(cancellationToken);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
if (!await connection.CreateMigrationsTable(options, cancellationToken))
if (!await connection.CreateMigrationsTable(options, cancellationToken).ConfigureAwait(false))
return false;
return await connection.Migrate(options, cancellationToken);
return await connection.Migrate(options, cancellationToken).ConfigureAwait(false);
}
finally
{
await connection.CloseAsync();
await connection.CloseAsync().ConfigureAwait(false);
}
}
@@ -87,7 +87,7 @@ namespace Microsoft.EntityFrameworkCore
{
try
{
await connection.OpenAsync(cancellationToken);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
options.Logger?.LogInformation("Database connection available");
return;
}
@@ -96,7 +96,7 @@ namespace Microsoft.EntityFrameworkCore
// keep things quiet
try
{
await Task.Delay(options.WaitDelay, cancellationToken);
await Task.Delay(options.WaitDelay, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
@@ -109,7 +109,7 @@ namespace Microsoft.EntityFrameworkCore
}
finally
{
await connection.CloseAsync();
await connection.CloseAsync().ConfigureAwait(false);
}
}
}
@@ -192,7 +192,7 @@ BEGIN
END;"
};
await command.ExecuteNonQueryAsync(cancellationToken);
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
return true;
}
catch (Exception ex)
@@ -235,9 +235,9 @@ END;"
DatabaseProvider.SQLServer => $"SELECT [schema_file] FROM [{options.MigrationsTableName}];",
_ => $@"SELECT ""schema_file"" FROM ""{options.MigrationsTableName}"";",
};
using (var reader = await command.ExecuteReaderAsync(cancellationToken))
using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
while (await reader.ReadAsync(cancellationToken))
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
migratedFiles.Add(reader.GetString(0));
}
@@ -246,7 +246,7 @@ END;"
{
// remove path including the separator
string fileName = migrationFile.Replace(options.Path, "")[1..];
using var transaction = await connection.BeginTransactionAsync(cancellationToken);
using var transaction = await connection.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
try
{
// max length in the database: 250 chars
@@ -263,13 +263,13 @@ END;"
string sqlScript = null;
if (options.SourceAssembly == null)
{
sqlScript = await File.ReadAllTextAsync(migrationFile, cancellationToken);
sqlScript = await File.ReadAllTextAsync(migrationFile, cancellationToken).ConfigureAwait(false);
}
else
{
using var stream = options.SourceAssembly.GetManifestResourceStream(migrationFile);
using var sr = new StreamReader(stream);
sqlScript = await sr.ReadToEndAsync();
sqlScript = await sr.ReadToEndAsync().ConfigureAwait(false);
}
if (string.IsNullOrWhiteSpace(sqlScript))
@@ -278,7 +278,7 @@ END;"
options.Logger?.LogDebug($" Migrating file '{fileName}' started");
command.Transaction = transaction;
await command.ExecuteScript(sqlScript, cancellationToken);
await command.ExecuteScript(sqlScript, cancellationToken).ConfigureAwait(false);
command.CommandText = connection.GetProviderType() switch
{
@@ -286,15 +286,15 @@ END;"
DatabaseProvider.SQLServer => $"INSERT INTO [{options.MigrationsTableName}] ([schema_file], [installed_at]) VALUES ('{trimmedFileName.Replace("'", "\\'")}', '{DateTime.UtcNow:yyyy-MM-dd HH:mm}');",
_ => $@"INSERT INTO ""{options.MigrationsTableName}"" (""schema_file"", ""installed_at"") VALUES ('{trimmedFileName.Replace("'", "\\'")}', '{DateTime.UtcNow:yyyy-MM-dd HH:mm}');",
};
await command.ExecuteNonQueryAsync(cancellationToken);
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
await transaction.CommitAsync(cancellationToken);
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
command.Transaction = null;
options.Logger?.LogDebug($" Migrating file '{fileName}' successful");
}
catch (Exception ex)
{
await transaction.RollbackAsync(cancellationToken);
await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
options.Logger?.LogError($"Migrating file '{fileName}' failed: {ex.InnerException?.Message ?? ex.Message}");
return false;
}
@@ -330,7 +330,7 @@ END;"
if (!string.IsNullOrWhiteSpace(pt))
{
command.CommandText = pt;
affectedRows += await command.ExecuteNonQueryAsync(cancellationToken);
affectedRows += await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}
return affectedRows;
@@ -338,7 +338,7 @@ END;"
else
{
command.CommandText = text;
return await command.ExecuteNonQueryAsync(cancellationToken);
return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}