Skip to content

Disable .ConfigureAwait(false) by conditional compilation constant DO_CONFIGURE_AWAIT_FALSE #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@
</ItemGroup>

<Import Condition="Exists('User.Directory.Build.props')" Project="User.Directory.Build.props" />

<PropertyGroup>
<DefineConstants Condition="'$(DO_CONFIGURE_AWAIT_FALSE)'!='false'">$(DefineConstants);DO_CONFIGURE_AWAIT_FALSE</DefineConstants>
</PropertyGroup>
</Project>
25 changes: 13 additions & 12 deletions Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using Xtensive.Core;
using SqlServerConnection = Microsoft.Data.SqlClient.SqlConnection;

namespace Xtensive.Sql.Drivers.SqlServer
Expand Down Expand Up @@ -143,11 +144,11 @@ public override async Task RollbackAsync(CancellationToken token = default)

try {
if (!IsTransactionZombied()) {
await ActiveTransaction.RollbackAsync(token).ConfigureAwait(false);
await ActiveTransaction.RollbackAsync(token).ConfigureAwaitFalse();
}
}
finally {
await ActiveTransaction.DisposeAsync().ConfigureAwait(false);
await ActiveTransaction.DisposeAsync().ConfigureAwaitFalse();
ClearActiveTransaction();
}
}
Expand Down Expand Up @@ -265,12 +266,12 @@ private async Task OpenWithCheckFastAsync(string checkQueryString, CancellationT

while (!connectionChecked) {
cancellationToken.ThrowIfCancellationRequested();
await underlyingConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
await underlyingConnection.OpenAsync(cancellationToken).ConfigureAwaitFalse();
try {
var command = underlyingConnection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = checkQueryString;
_ = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwaitFalse();
}
connectionChecked = true;
}
Expand Down Expand Up @@ -308,27 +309,27 @@ private async Task OpenWithCheckAndNotificationAsync(string checkQueryString,

await SqlHelper.NotifyConnectionOpeningAsync(accessors,
UnderlyingConnection, (!connectionChecked && !restoreTriggered), cancellationToken)
.ConfigureAwait(false);
.ConfigureAwaitFalse();

await underlyingConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
await underlyingConnection.OpenAsync(cancellationToken).ConfigureAwaitFalse();
try {
await SqlHelper.NotifyConnectionInitializingAsync(accessors,
UnderlyingConnection, checkQueryString, (!connectionChecked && !restoreTriggered), cancellationToken)
.ConfigureAwait(false);
.ConfigureAwaitFalse();

var command = underlyingConnection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = checkQueryString;
_ = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwaitFalse();
}
connectionChecked = true;
await SqlHelper.NotifyConnectionOpenedAsync(accessors, UnderlyingConnection, (!connectionChecked && !restoreTriggered), cancellationToken)
.ConfigureAwait(false);
.ConfigureAwaitFalse();
}
catch (Exception exception) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(accessors,
UnderlyingConnection, exception, (!connectionChecked && !restoreTriggered), cancellationToken)
.ConfigureAwait(false);
.ConfigureAwaitFalse();

if (InternalHelpers.ShouldRetryOn(exception)) {
if (restoreTriggered) {
Expand Down
91 changes: 46 additions & 45 deletions Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using Xtensive.Core;
using Xtensive.Orm;
using Xtensive.Sql.Info;
using Xtensive.SqlServer.Resources;
Expand Down Expand Up @@ -65,18 +66,18 @@ private static async Task<ErrorMessageParser> CreateMessageParserAsync(
{
bool isEnglish;
var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = LangIdQuery;
isEnglish = (await command.ExecuteScalarAsync(token).ConfigureAwait(false)).ToString()=="0";
isEnglish = (await command.ExecuteScalarAsync(token).ConfigureAwaitFalse()).ToString()=="0";
}

var templates = new Dictionary<int, string>();
command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = MessagesQuery;
var reader = await command.ExecuteReaderAsync(token).ConfigureAwait(false);
await using (reader.ConfigureAwait(false)) {
while (await reader.ReadAsync(token).ConfigureAwait(false)) {
var reader = await command.ExecuteReaderAsync(token).ConfigureAwaitFalse();
await using (reader.ConfigureAwaitFalse()) {
while (await reader.ReadAsync(token).ConfigureAwaitFalse()) {
ReadMessageTemplate(reader, templates);
}
}
Expand All @@ -97,9 +98,9 @@ private static bool IsAzure(SqlServerConnection connection)
private static async Task<bool> IsAzureAsync(SqlServerConnection connection, CancellationToken token)
{
var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = VersionQuery;
return ((string) await command.ExecuteScalarAsync(token).ConfigureAwait(false))
return ((string) await command.ExecuteScalarAsync(token).ConfigureAwaitFalse())
.IndexOf("Azure", StringComparison.Ordinal) >= 0;
}
}
Expand Down Expand Up @@ -170,23 +171,23 @@ protected override async Task<SqlDriver> CreateDriverAsync(
var isPooingOn = !IsPoolingOff(connectionString);
configuration.EnsureConnectionIsAlive &= isPooingOn;

var connection = await CreateAndOpenConnectionAsync(connectionString, configuration, token).ConfigureAwait(false);
await using (connection.ConfigureAwait(false)) {
var connection = await CreateAndOpenConnectionAsync(connectionString, configuration, token).ConfigureAwaitFalse();
await using (connection.ConfigureAwaitFalse()) {
var isEnsureAlive = configuration.EnsureConnectionIsAlive;
var forcedServerVersion = configuration.ForcedServerVersion;
var isForcedVersion = !string.IsNullOrEmpty(forcedServerVersion);
var isForcedAzure = isForcedVersion && forcedServerVersion.Equals("azure", StringComparison.OrdinalIgnoreCase);
var isAzure = isForcedAzure
|| (!isForcedVersion && await IsAzureAsync(connection, token).ConfigureAwait(false));
|| (!isForcedVersion && await IsAzureAsync(connection, token).ConfigureAwaitFalse());
var parser = isAzure
? new ErrorMessageParser()
: await CreateMessageParserAsync(connection, token).ConfigureAwait(false);
: await CreateMessageParserAsync(connection, token).ConfigureAwaitFalse();

var versionString = isForcedVersion
? isForcedAzure ? "10.0.0.0" : forcedServerVersion
: connection.ServerVersion ?? string.Empty;
var version = new Version(versionString);
var defaultSchema = await GetDefaultSchemaAsync(connection, token: token).ConfigureAwait(false);
var defaultSchema = await GetDefaultSchemaAsync(connection, token: token).ConfigureAwaitFalse();

return CreateDriverInstance(connectionString, isAzure, version, defaultSchema, parser, isEnsureAlive);
}
Expand Down Expand Up @@ -261,20 +262,20 @@ private static async Task<SqlServerConnection> CreateAndOpenConnectionAsync(

if (!configuration.EnsureConnectionIsAlive) {
if (configuration.DbConnectionAccessors.Count == 0)
await OpenConnectionFast(connection, initScript, true, token).ConfigureAwait(false);
await OpenConnectionFast(connection, initScript, true, token).ConfigureAwaitFalse();
else
await OpenConnectionWithNotification(connection, configuration, true, token).ConfigureAwait(false);
await OpenConnectionWithNotification(connection, configuration, true, token).ConfigureAwaitFalse();
return connection;
}

var testQuery = string.IsNullOrEmpty(initScript)
? CheckConnectionQuery
: initScript;
if (configuration.DbConnectionAccessors.Count == 0)
return await EnsureConnectionIsAliveFast(connection, testQuery, true, token).ConfigureAwait(false);
return await EnsureConnectionIsAliveFast(connection, testQuery, true, token).ConfigureAwaitFalse();
else
return await EnsureConnectionIsAliveWithNotification(connection, testQuery, configuration.DbConnectionAccessors, true, token)
.ConfigureAwait(false);
.ConfigureAwaitFalse();
}

private static async ValueTask OpenConnectionFast(SqlServerConnection connection,
Expand All @@ -285,8 +286,8 @@ private static async ValueTask OpenConnectionFast(SqlServerConnection connection
SqlHelper.ExecuteInitializationSql(connection, sqlScript);
}
else {
await connection.OpenAsync(token).ConfigureAwait(false);
await SqlHelper.ExecuteInitializationSqlAsync(connection, sqlScript, token).ConfigureAwait(false);
await connection.OpenAsync(token).ConfigureAwaitFalse();
await SqlHelper.ExecuteInitializationSqlAsync(connection, sqlScript, token).ConfigureAwaitFalse();
}
}

Expand Down Expand Up @@ -362,28 +363,28 @@ private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveFast(
}
else {
try {
await connection.OpenAsync(token).ConfigureAwait(false);
await connection.OpenAsync(token).ConfigureAwaitFalse();

var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}

return connection;
}
catch (Exception exception) {
try {
await connection.CloseAsync().ConfigureAwait(false);
await connection.DisposeAsync().ConfigureAwait(false);
await connection.CloseAsync().ConfigureAwaitFalse();
await connection.DisposeAsync().ConfigureAwaitFalse();
}
catch {
// ignored
}

if (InternalHelpers.ShouldRetryOn(exception)) {
var (isReconnected, newConnection) =
await TryReconnectFast(connection.ConnectionString, query, isAsync, token).ConfigureAwait(false);
await TryReconnectFast(connection.ConnectionString, query, isAsync, token).ConfigureAwaitFalse();
if (isReconnected) {
return newConnection;
}
Expand Down Expand Up @@ -434,40 +435,40 @@ private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveWithN
}
}
else {
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessos, connection, false, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessos, connection, false, token).ConfigureAwaitFalse();

try {
await connection.OpenAsync(token).ConfigureAwait(false);
await connection.OpenAsync(token).ConfigureAwaitFalse();

await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessos, connection, query, false, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessos, connection, query, false, token).ConfigureAwaitFalse();

var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}

await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessos, connection, false, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessos, connection, false, token).ConfigureAwaitFalse();
return connection;
}
catch (Exception exception) {
var retryToConnect = InternalHelpers.ShouldRetryOn(exception);
if (!retryToConnect) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessos, connection, exception, false, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessos, connection, exception, false, token).ConfigureAwaitFalse();
}

var connectionString = connection.ConnectionString;
try {
await connection.CloseAsync().ConfigureAwait(false);
await connection.DisposeAsync().ConfigureAwait(false);
await connection.CloseAsync().ConfigureAwaitFalse();
await connection.DisposeAsync().ConfigureAwaitFalse();
}
catch {
// ignored
}

if (retryToConnect) {
var (isReconnected, newConnection) =
await TryReconnectWithNotification(connectionString, query, connectionAccessos, isAsync, token).ConfigureAwait(false);
await TryReconnectWithNotification(connectionString, query, connectionAccessos, isAsync, token).ConfigureAwaitFalse();
if (isReconnected) {
return newConnection;
}
Expand Down Expand Up @@ -499,12 +500,12 @@ private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveWithN
}
else {
try {
await connection.OpenAsync(token).ConfigureAwait(false);
await connection.OpenAsync(token).ConfigureAwaitFalse();

var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}

return (true, connection);
Expand Down Expand Up @@ -543,24 +544,24 @@ private static async ValueTask<SqlServerConnection> EnsureConnectionIsAliveWithN
}
}
else {
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessors, connection, true, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpeningAsync(connectionAccessors, connection, true, token).ConfigureAwaitFalse();

try {
await connection.OpenAsync(token).ConfigureAwait(false);
await connection.OpenAsync(token).ConfigureAwaitFalse();

await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessors, connection, query, true, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionInitializingAsync(connectionAccessors, connection, query, true, token).ConfigureAwaitFalse();

var command = connection.CreateCommand();
await using (command.ConfigureAwait(false)) {
await using (command.ConfigureAwaitFalse()) {
command.CommandText = query;
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwait(false);
_ = await command.ExecuteNonQueryAsync(token).ConfigureAwaitFalse();
}

await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessors, connection, true, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpenedAsync(connectionAccessors, connection, true, token).ConfigureAwaitFalse();
return (true, connection);
}
catch (Exception exception) {
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessors, connection, exception, true, token).ConfigureAwait(false);
await SqlHelper.NotifyConnectionOpeningFailedAsync(connectionAccessors, connection, exception, true, token).ConfigureAwaitFalse();
await connection.DisposeAsync();
return (false, null);
}
Expand Down
Loading