@@ -167,8 +167,9 @@ public virtual void Open()
167167 {
168168 EnsureIsNotDisposed ( ) ;
169169 var connectionHandlers = Extensions . Get < ConnectionHandlersExtension > ( ) ;
170- if ( connectionHandlers == null )
170+ if ( connectionHandlers == null ) {
171171 UnderlyingConnection . Open ( ) ;
172+ }
172173 else {
173174 var handlers = connectionHandlers . Handlers ;
174175 SqlHelper . NotifyConnectionOpening ( handlers , UnderlyingConnection ) ;
@@ -230,11 +231,27 @@ public virtual void OpenAndInitialize(string initializationScript)
230231 /// to ensure that all asynchronous operations have completed.</remarks>
231232 /// <param name="cancellationToken">Token to control cancellation.</param>
232233 /// <returns>Awaitable task.</returns>
233- public virtual Task OpenAsync ( CancellationToken cancellationToken )
234+ public virtual async Task OpenAsync ( CancellationToken cancellationToken )
234235 {
235236 cancellationToken . ThrowIfCancellationRequested ( ) ;
236237 EnsureIsNotDisposed ( ) ;
237- return UnderlyingConnection . OpenAsync ( cancellationToken ) ;
238+ var connectionHandlers = Extensions . Get < ConnectionHandlersExtension > ( ) ;
239+
240+ if ( connectionHandlers == null ) {
241+ await UnderlyingConnection . OpenAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
242+ }
243+ else {
244+ var handlers = connectionHandlers . Handlers ;
245+ await SqlHelper . NotifyConnectionOpeningAsync ( handlers , UnderlyingConnection , false , cancellationToken ) ;
246+ try {
247+ await UnderlyingConnection . OpenAsync ( cancellationToken ) ;
248+ await SqlHelper . NotifyConnectionOpenedAsync ( handlers , UnderlyingConnection , false , cancellationToken ) ;
249+ }
250+ catch ( Exception ex ) {
251+ await SqlHelper . NotifyConnectionOpeningFailedAsync ( handlers , UnderlyingConnection , ex , false , cancellationToken ) ;
252+ throw ;
253+ }
254+ }
238255 }
239256
240257 /// <summary>
@@ -249,21 +266,52 @@ public virtual async Task OpenAndInitializeAsync(string initializationScript, Ca
249266 {
250267 token . ThrowIfCancellationRequested ( ) ;
251268 EnsureIsNotDisposed ( ) ;
252- await UnderlyingConnection . OpenAsync ( token ) . ConfigureAwait ( false ) ;
253- if ( string . IsNullOrEmpty ( initializationScript ) ) {
254- return ;
255- }
269+ var connectionHandlers = Extensions . Get < ConnectionHandlersExtension > ( ) ;
270+ if ( connectionHandlers == null ) {
271+ await UnderlyingConnection . OpenAsync ( token ) . ConfigureAwait ( false ) ;
272+ if ( string . IsNullOrEmpty ( initializationScript ) ) {
273+ return ;
274+ }
256275
257- try {
258- var command = UnderlyingConnection . CreateCommand ( ) ;
259- await using ( command . ConfigureAwait ( false ) ) {
260- command . CommandText = initializationScript ;
261- await command . ExecuteNonQueryAsync ( token ) . ConfigureAwait ( false ) ;
276+ try {
277+ var command = UnderlyingConnection . CreateCommand ( ) ;
278+ await using ( command . ConfigureAwait ( false ) ) {
279+ command . CommandText = initializationScript ;
280+ _ = await command . ExecuteNonQueryAsync ( token ) . ConfigureAwait ( false ) ;
281+ }
282+ }
283+ catch ( OperationCanceledException ) {
284+ await UnderlyingConnection . CloseAsync ( ) . ConfigureAwait ( false ) ;
285+ throw ;
262286 }
263287 }
264- catch ( OperationCanceledException ) {
265- await UnderlyingConnection . CloseAsync ( ) . ConfigureAwait ( false ) ;
266- throw ;
288+ else {
289+ var handlers = connectionHandlers . Handlers ;
290+ await SqlHelper . NotifyConnectionOpeningAsync ( handlers , UnderlyingConnection , false , token ) ;
291+ await UnderlyingConnection . OpenAsync ( token ) . ConfigureAwait ( false ) ;
292+ if ( string . IsNullOrEmpty ( initializationScript ) ) {
293+ await SqlHelper . NotifyConnectionOpenedAsync ( handlers , UnderlyingConnection , false , token ) ;
294+ return ;
295+ }
296+
297+ try {
298+ await SqlHelper . NotifyConnectionInitializingAsync ( handlers , UnderlyingConnection , initializationScript , false , token ) ;
299+ var command = UnderlyingConnection . CreateCommand ( ) ;
300+ await using ( command . ConfigureAwait ( false ) ) {
301+ command . CommandText = initializationScript ;
302+ _ = await command . ExecuteNonQueryAsync ( token ) . ConfigureAwait ( false ) ;
303+ }
304+ await SqlHelper . NotifyConnectionOpenedAsync ( handlers , UnderlyingConnection , false , token ) ;
305+ }
306+ catch ( OperationCanceledException ex ) {
307+ await SqlHelper . NotifyConnectionOpeningFailedAsync ( handlers , UnderlyingConnection , ex , false , token ) ;
308+ await UnderlyingConnection . CloseAsync ( ) . ConfigureAwait ( false ) ;
309+ throw ;
310+ }
311+ catch ( Exception ex ) {
312+ await SqlHelper . NotifyConnectionOpeningFailedAsync ( handlers , UnderlyingConnection , ex , false , token ) ;
313+ throw ;
314+ }
267315 }
268316 }
269317
0 commit comments