Last active
October 29, 2015 15:59
-
-
Save imranbaloch/27c30178cf4daed1b0b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//await connection.OpenWithRetryAsync(retryPolicy).ConfigureAwait(false); | |
//var reader = await command.ExecuteReaderWithRetryAsync(retryPolicy).ConfigureAwait(false); | |
public static Task OpenWithRetryAsync(this SqlConnection connection, | |
RetryPolicy retryPolicy) | |
{ | |
return retryPolicy.ExecuteAsync(connection.OpenAsync); | |
} | |
public static class SqlCommandExtensions | |
{ | |
public static async Task<SqlDataReader> ExecuteReaderWithRetryAsync(this SqlCommand command, | |
RetryPolicy retryPolicy) | |
{ | |
return (SqlDataReader) await command.ExecuteWithRetryAsync(retryPolicy, ExecutionType.Reader).ConfigureAwait(false); | |
} | |
public static async Task<int> ExecuteNonQueryWithRetryAsync(this SqlCommand command, | |
RetryPolicy retryPolicy) | |
{ | |
return (int) await command.ExecuteWithRetryAsync(retryPolicy, ExecutionType.NonQuery).ConfigureAwait(false); | |
} | |
public static async Task<object> ExecuteScalarWithRetryAsync(this SqlCommand command, | |
RetryPolicy retryPolicy) | |
{ | |
return await command.ExecuteWithRetryAsync(retryPolicy, ExecutionType.Scaler).ConfigureAwait(false); | |
} | |
public static async Task<object> ExecuteWithRetryAsync(this SqlCommand command, | |
RetryPolicy retryPolicy, | |
ExecutionType executionType) | |
{ | |
return await retryPolicy.ExecuteAsync(async () => | |
{ | |
var hasOpenConnection = await EnsureValidConnectionAsync(command, retryPolicy).ConfigureAwait(false); | |
try | |
{ | |
switch (executionType) | |
{ | |
case ExecutionType.Reader: | |
return await command.ExecuteReaderAsync().ConfigureAwait(false); | |
case ExecutionType.NonQuery: | |
return await command.ExecuteNonQueryAsync().ConfigureAwait(false); | |
default: | |
return await command.ExecuteScalarAsync().ConfigureAwait(false); | |
} | |
} | |
catch (Exception exception) | |
{ | |
if (hasOpenConnection && command.Connection != null && command.Connection.State == ConnectionState.Open)// if we explcitly opened then we have to close it | |
{ | |
command.Connection.Close(); | |
} | |
throw; | |
} | |
}).ConfigureAwait(false); | |
} | |
private static async Task<bool> EnsureValidConnectionAsync(SqlCommand command, | |
RetryPolicy retryPolicy) | |
{ | |
if (command.Connection.State != ConnectionState.Open) | |
{ | |
await command.Connection.OpenWithRetryAsync(retryPolicy); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for reviewing and suggesting.