Method OpenAsync
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
OpenAsync(IDbConnection, CancellationToken)
Opens the connection by calling OpenAsync(CancellationToken) if it is a DbConnection, and Open() otherwise.
public static ValueTask OpenAsync(this IDbConnection connection, CancellationToken cancellationToken = default)
Parameters
connectionIDbConnectionThe connection to open.
cancellationTokenCancellationTokenA token that cancels opening a DbConnection. It is ignored for any other connection.
Returns
- ValueTask
A task that represents the asynchronous operation.
Remarks
Unlike EnsureOpenAsync(IDbConnection, CancellationToken), this method does not check the state of the connection first, and does not return it.
Exceptions
- ArgumentNullException
connectionis null. It is thrown by the call, not through the returned task.
OpenAsync<T>(IDbConnectionFactory, Func<IDbConnection, ValueTask<T>>, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask<T> OpenAsync<T>(this IDbConnectionFactory connectionFactory, Func<IDbConnection, ValueTask<T>> action, CancellationToken cancellationToken = default)
Parameters
connectionFactoryIDbConnectionFactoryThe connection factory to create the connection from.
actionFunc<IDbConnection, ValueTask<T>>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by the action, produced after the connection has been released.
Type Parameters
TThe type of the value returned by the action.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync(IDbConnectionFactory, Func<IDbConnection, ValueTask>, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask OpenAsync(this IDbConnectionFactory connectionFactory, Func<IDbConnection, ValueTask> action, CancellationToken cancellationToken = default)
Parameters
connectionFactoryIDbConnectionFactoryThe connection factory to create the connection from.
actionFunc<IDbConnection, ValueTask>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask
A task that represents the asynchronous operation. It completes after the action has completed and the connection has been released.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection, T>(IDbConnectionFactory<TConnection>, Func<TConnection, ValueTask<T>>, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask<T> OpenAsync<TConnection, T>(this IDbConnectionFactory<TConnection> connectionFactory, Func<TConnection, ValueTask<T>> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionFactoryIDbConnectionFactory<TConnection>The connection factory to create the connection from.
actionFunc<TConnection, ValueTask<T>>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by the action, produced after the connection has been released.
Type Parameters
TConnectionThe type of the connection.
TThe type of the value returned by the action.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection>(IDbConnectionFactory<TConnection>, Func<TConnection, ValueTask>, CancellationToken)
Creates a connection from the factory, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask OpenAsync<TConnection>(this IDbConnectionFactory<TConnection> connectionFactory, Func<TConnection, ValueTask> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionFactoryIDbConnectionFactory<TConnection>The connection factory to create the connection from.
actionFunc<TConnection, ValueTask>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask
A task that represents the asynchronous operation. It completes after the action has completed and the connection has been released.
Type Parameters
TConnectionThe type of the connection.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<T>(IDbConnectionPool, Func<IDbConnection, ConnectionState, ValueTask<T>>, CancellationToken)
Asynchronously takes a connection from the pool, opens it if needed, invokes the action with the connection and the state it was in before being opened, and then gives the connection back to the pool.
public static ValueTask<T> OpenAsync<T>(this IDbConnectionPool connectionPool, Func<IDbConnection, ConnectionState, ValueTask<T>> action, CancellationToken cancellationToken = default)
Parameters
connectionPoolIDbConnectionPoolThe connection pool to take the connection from.
actionFunc<IDbConnection, ConnectionState, ValueTask<T>>The asynchronous action to execute. It receives the connection and the ConnectionState the connection was in before it was opened.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by the action, produced after the connection has been released.
Type Parameters
TThe type of the value returned by the action.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been given back to the pool. If opening the connection throws, it is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPooloractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync(IDbConnectionPool, Func<IDbConnection, ConnectionState, ValueTask>, CancellationToken)
Asynchronously takes a connection from the pool, opens it if needed, invokes the action with the connection and the state it was in before being opened, and then gives the connection back to the pool.
public static ValueTask OpenAsync(this IDbConnectionPool connectionPool, Func<IDbConnection, ConnectionState, ValueTask> action, CancellationToken cancellationToken = default)
Parameters
connectionPoolIDbConnectionPoolThe connection pool to take the connection from.
actionFunc<IDbConnection, ConnectionState, ValueTask>The asynchronous action to execute. It receives the connection and the ConnectionState the connection was in before it was opened.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask
A task that represents the asynchronous operation. It completes after the action has completed and the connection has been released.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been given back to the pool. If opening the connection throws, it is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPooloractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection, T>(IDbConnectionPool<TConnection>, Func<TConnection, ConnectionState, ValueTask<T>>, CancellationToken)
Asynchronously takes a connection from the pool, opens it if needed, invokes the action with the connection and the state it was in before being opened, and then gives the connection back to the pool.
public static ValueTask<T> OpenAsync<TConnection, T>(this IDbConnectionPool<TConnection> connectionPool, Func<TConnection, ConnectionState, ValueTask<T>> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionPoolIDbConnectionPool<TConnection>The connection pool to take the connection from.
actionFunc<TConnection, ConnectionState, ValueTask<T>>The asynchronous action to execute. It receives the connection and the ConnectionState the connection was in before it was opened.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by the action, produced after the connection has been released.
Type Parameters
TConnectionThe type of the connection.
TThe type of the value returned by the action.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been given back to the pool. If opening the connection throws, it is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPooloractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection>(IDbConnectionPool<TConnection>, Func<TConnection, ConnectionState, ValueTask>, CancellationToken)
Asynchronously takes a connection from the pool, opens it if needed, invokes the action with the connection and the state it was in before being opened, and then gives the connection back to the pool.
public static ValueTask OpenAsync<TConnection>(this IDbConnectionPool<TConnection> connectionPool, Func<TConnection, ConnectionState, ValueTask> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionPoolIDbConnectionPool<TConnection>The connection pool to take the connection from.
actionFunc<TConnection, ConnectionState, ValueTask>The asynchronous action to execute. It receives the connection and the ConnectionState the connection was in before it was opened.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask
A task that represents the asynchronous operation. It completes after the action has completed and the connection has been released.
Type Parameters
TConnectionThe type of the connection.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been given back to the pool. If opening the connection throws, it is given back before the exception propagates.
Exceptions
- ArgumentNullException
connectionPooloractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection, T>(Func<TConnection>, Func<TConnection, ValueTask<T>>, CancellationToken)
Invokes the function to create a connection, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask<T> OpenAsync<TConnection, T>(this Func<TConnection> connectionFactory, Func<TConnection, ValueTask<T>> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionFactoryFunc<TConnection>The function that creates the connection.
actionFunc<TConnection, ValueTask<T>>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask<T>
A task that represents the asynchronous operation; its result is the value returned by the action, produced after the connection has been released.
Type Parameters
TConnectionThe type of the connection.
TThe type of the value returned by the action.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.
OpenAsync<TConnection>(Func<TConnection>, Func<TConnection, ValueTask>, CancellationToken)
Invokes the function to create a connection, asynchronously opens it if needed, invokes the action, and then disposes the connection.
public static ValueTask OpenAsync<TConnection>(this Func<TConnection> connectionFactory, Func<TConnection, ValueTask> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection
Parameters
connectionFactoryFunc<TConnection>The function that creates the connection.
actionFunc<TConnection, ValueTask>The asynchronous action to execute. It receives the connection.
cancellationTokenCancellationTokenA token that cancels opening the connection.
Returns
- ValueTask
A task that represents the asynchronous operation. It completes after the action has completed and the connection has been released.
Type Parameters
TConnectionThe type of the connection.
Remarks
Useful for single-line operations. Exceptions thrown by the action propagate after the connection has been disposed. If opening the connection throws, it is disposed before the exception propagates.
Exceptions
- ArgumentNullException
connectionFactoryoractionis null. The exception is reported through the returned task.- OperationCanceledException
cancellationTokenwas canceled before or while the connection was being opened.