Method EnsureOpenScopeAsync
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
EnsureOpenScopeAsync<TConnection>(TConnection, CancellationToken)
Asynchronously opens the connection if it is not open (a broken connection is closed first) and returns a ConnectionScope<TConnection> that puts it back when disposed: a connection this call opened is closed again, and one that was already open is left open.
public static ValueTask<ConnectionScope<TConnection>> EnsureOpenScopeAsync<TConnection>(this TConnection connection, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionTConnectionThe connection to open.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<ConnectionScope<TConnection>>
The connection and the state it was found in; DisposeAsync() closes it again if the scope opened it.
Type Parameters
TConnectionThe type of the connection.
Remarks
await using var _ = await connection.EnsureOpenScopeAsync(cancellationToken);
The scope is usually only there to be disposed; keep it
(var scope = ..., then var (conn, priorState) = scope;) when you also want the state the
connection was found in. The same rule as EnsureOpenAsync(DbConnection, CancellationToken),
which returns the prior state for a caller that does the closing itself.
A connection that is not a DbConnection is opened synchronously.
Always awaits with ConfigureAwait(false).
If the token is canceled the connection it opened is closed again and no scope is returned.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.- ArgumentNullException
connectionis null. The exception is reported through the returned task.
EnsureOpenScopeAsync(IDbConnectionFactory, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, and returns a ConnectionScope<TConnection> that disposes it when the scope is disposed.
public static ValueTask<ConnectionScope<IDbConnection>> EnsureOpenScopeAsync(this IDbConnectionFactory connectionFactory, CancellationToken cancellationToken = default)
Parameters
connectionFactoryIDbConnectionFactoryThe connection factory to create the connection from.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<ConnectionScope<IDbConnection>>
The connection, and the state it was in before it was opened.
Remarks
await using var scope = await factory.EnsureOpenScopeAsync(cancellationToken);
var connection = scope.Connection;
The scope form of OpenAsync<T>(IDbConnectionFactory, Func<IDbConnection, ValueTask<T>>, CancellationToken).
The scope's DisposeAsync() calls DisposeAsync on a connection
that has one. If opening throws or cancellationToken is canceled, the connection is
disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
EnsureOpenScopeAsync<TConnection>(IDbConnectionFactory<TConnection>, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, and returns a ConnectionScope<TConnection> that disposes it when the scope is disposed.
public static ValueTask<ConnectionScope<TConnection>> EnsureOpenScopeAsync<TConnection>(this IDbConnectionFactory<TConnection> connectionFactory, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionFactoryIDbConnectionFactory<TConnection>The connection factory to create the connection from.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<ConnectionScope<TConnection>>
The connection, and the state it was in before it was opened.
Type Parameters
TConnectionThe type of the connection.
Remarks
await using var scope = await factory.EnsureOpenScopeAsync(cancellationToken);
var connection = scope.Connection;
The scope form of OpenAsync<T>(IDbConnectionFactory, Func<IDbConnection, ValueTask<T>>, CancellationToken).
The scope's DisposeAsync() calls DisposeAsync on a connection
that has one. If opening throws or cancellationToken is canceled, the connection is
disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
EnsureOpenScopeAsync(IDbConnectionPool, CancellationToken)
Takes a connection from the pool, asynchronously opens it if needed, and returns a ConnectionScope<TConnection> that gives it back to the pool when the scope is disposed.
public static ValueTask<ConnectionScope<IDbConnection>> EnsureOpenScopeAsync(this IDbConnectionPool connectionPool, CancellationToken cancellationToken = default)
Parameters
connectionPoolIDbConnectionPoolThe connection pool to take the connection from.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<ConnectionScope<IDbConnection>>
The connection, and the state it was in before it was opened.
Remarks
await using var scope = await pool.EnsureOpenScopeAsync(cancellationToken);
var connection = scope.Connection;
The scope form of OpenAsync<T>(IDbConnectionPool, Func<IDbConnection, ConnectionState, ValueTask<T>>, CancellationToken).
Giving back is the pool's, and is synchronous. If opening throws or cancellationToken is
canceled, the connection is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPoolis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
EnsureOpenScopeAsync<TConnection>(IDbConnectionPool<TConnection>, CancellationToken)
Takes a connection from the pool, asynchronously opens it if needed, and returns a ConnectionScope<TConnection> that gives it back to the pool when the scope is disposed.
public static ValueTask<ConnectionScope<TConnection>> EnsureOpenScopeAsync<TConnection>(this IDbConnectionPool<TConnection> connectionPool, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionPoolIDbConnectionPool<TConnection>The connection pool to take the connection from.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<ConnectionScope<TConnection>>
The connection, and the state it was in before it was opened.
Type Parameters
TConnectionThe type of the connection.
Remarks
await using var scope = await pool.EnsureOpenScopeAsync(cancellationToken);
var connection = scope.Connection;
The scope form of OpenAsync<T>(IDbConnectionPool, Func<IDbConnection, ConnectionState, ValueTask<T>>, CancellationToken).
Giving back is the pool's, and is synchronous. If opening throws or cancellationToken is
canceled, the connection is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPoolis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.