Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/SqlAsyncCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public SqlAsyncCollector(IConfiguration configuration, SqlAttribute attribute, I
using (SqlConnection connection = BuildConnection(attribute.ConnectionStringSetting, configuration))
{
this._logger.LogDebugWithThreadId("BEGIN OpenSqlAsyncCollectorVerifyDatabaseSupportedConnection");
connection.Open();
connection.OpenAsyncWithSqlErrorHandling(CancellationToken.None).Wait();
this._logger.LogDebugWithThreadId("END OpenSqlAsyncCollectorVerifyDatabaseSupportedConnection");
VerifyDatabaseSupported(connection, logger, CancellationToken.None).Wait();
}
Expand Down
30 changes: 30 additions & 0 deletions src/SqlBindingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,35 @@ public static async Task VerifyDatabaseSupported(SqlConnection connection, ILogg
}
}
}

/// <summary>
/// Opens a connection and handles some specific errors if they occur.
/// </summary>
/// <param name="connection">The connection to open</param>
/// <param name="cancellationToken">The cancellation token to pass to the OpenAsync call</param>
/// <returns>The task that will be completed when the connection is made</returns>
/// <exception cref="InvalidOperationException">Thrown if an error occurred that we want to wrap with more information</exception>
internal static async Task OpenAsyncWithSqlErrorHandling(this SqlConnection connection, CancellationToken cancellationToken)
{
try
{
await connection.OpenAsync(cancellationToken);
}
catch (Exception e)
{
SqlException sqlEx = e is AggregateException a ? a.InnerExceptions.OfType<SqlException>().First() :
e is SqlException s ? s :
null;
// Error number for:
// A connection was successfully established with the server, but then an error occurred during the login process.
// The certificate chain was issued by an authority that is not trusted.
// Add on some more information to help the user figure out how to solve it
if (sqlEx?.Number == -2146893019)
{
throw new InvalidOperationException("The default values for encryption on connections have been changed, please review your configuration to ensure you have the correct values for your server. See https://aka.ms/afsqlext-connection for more details.", e);
}
throw;
}
}
}
}
2 changes: 1 addition & 1 deletion src/SqlConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public virtual async Task<string> BuildItemFromAttributeAsync(SqlAttribute attri
{
adapter.SelectCommand = command;
this._logger.LogDebugWithThreadId("BEGIN OpenBuildItemFromAttributeAsyncConnection");
await connection.OpenAsync();
await connection.OpenAsyncWithSqlErrorHandling(CancellationToken.None);
this._logger.LogDebugWithThreadId("END OpenBuildItemFromAttributeAsyncConnection");
Dictionary<TelemetryPropertyName, string> props = connection.AsConnectionProps();
TelemetryInstance.TrackConvert(type, props);
Expand Down
2 changes: 1 addition & 1 deletion src/TriggerBinding/SqlTriggerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
using (var connection = new SqlConnection(this._connectionString))
{
this._logger.LogDebugWithThreadId("BEGIN OpenListenerConnection");
await connection.OpenAsync(cancellationToken);
await connection.OpenAsyncWithSqlErrorHandling(cancellationToken);
this._logger.LogDebugWithThreadId("END OpenListenerConnection");
this._telemetryProps.AddConnectionProps(connection);

Expand Down