Table of Contents

Method EnsureOpenAsync

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

EnsureOpenAsync(DbConnection, CancellationToken)

Asynchronously opens the connection if it is not open (a broken connection is closed first) and returns the prior state, so you can close it afterwards if it was not open.

public static ValueTask<ConnectionState> EnsureOpenAsync(this DbConnection connection, CancellationToken cancellationToken = default)

Parameters

connection DbConnection

The connection to open.

cancellationToken CancellationToken

A token that cancels opening the connection.

Returns

ValueTask<ConnectionState>

A task that represents the asynchronous operation; its result is the state the connection was in before this call.

Remarks

Always awaits with ConfigureAwait(false): the caller decides whether to return to its context. If the token is canceled after the connection has been opened, the connection is closed again before the exception is thrown.

Exceptions

OperationCanceledException

cancellationToken was canceled before or while the connection was being opened.

ArgumentNullException

connection is null. The exception is reported through the returned task.

EnsureOpenAsync(IDbConnection, CancellationToken)

Opens the connection if it is not open (a broken connection is closed first) and returns the prior state, asynchronously when the connection is a DbConnection.

public static ValueTask<ConnectionState> EnsureOpenAsync(this IDbConnection connection, CancellationToken cancellationToken = default)

Parameters

connection IDbConnection

The connection to open.

cancellationToken CancellationToken

A token that cancels opening the connection.

Returns

ValueTask<ConnectionState>

A task that represents the asynchronous operation; its result is the state the connection was in before this call.

Remarks

A connection that is not a DbConnection is opened synchronously by EnsureOpen(IDbConnection), after the token has been checked.

Exceptions

OperationCanceledException

cancellationToken was canceled before or while the connection was being opened.

ArgumentNullException

connection is null. The exception is reported through the returned task.