public override Task OpenAsync(CancellationToken cancellationToken) { PrepareStatisticsForNewConnection(); SqlStatistics statistics = null; try { statistics = SqlStatistics.StartTimer(Statistics); TaskCompletionSource <DbConnectionInternal> completion = new TaskCompletionSource <DbConnectionInternal>(); TaskCompletionSource <object> result = new TaskCompletionSource <object>(); if (cancellationToken.IsCancellationRequested) { result.SetCanceled(); return(result.Task); } bool completed; try { completed = TryOpen(completion); } catch (Exception e) { result.SetException(e); return(result.Task); } if (completed) { result.SetResult(null); } else { CancellationTokenRegistration registration = new CancellationTokenRegistration(); if (cancellationToken.CanBeCanceled) { registration = cancellationToken.Register(() => completion.TrySetCanceled()); } OpenAsyncRetry retry = new OpenAsyncRetry(this, completion, result, registration); _currentCompletion = new Tuple <TaskCompletionSource <DbConnectionInternal>, Task>(completion, result.Task); completion.Task.ContinueWith(retry.Retry, TaskScheduler.Default); return(result.Task); } return(result.Task); } finally { SqlStatistics.StopTimer(statistics); } }
public override Task OpenAsync(CancellationToken cancellationToken) { Guid operationId = s_diagnosticListener.WriteConnectionOpenBefore(this); PrepareStatisticsForNewConnection(); SqlStatistics statistics = null; try { statistics = SqlStatistics.StartTimer(Statistics); TaskCompletionSource <DbConnectionInternal> completion = new TaskCompletionSource <DbConnectionInternal>(); TaskCompletionSource <object> result = new TaskCompletionSource <object>(); if (s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlAfterOpenConnection) || s_diagnosticListener.IsEnabled(SqlClientDiagnosticListenerExtensions.SqlErrorOpenConnection)) { result.Task.ContinueWith((t) => { if (t.Exception != null) { s_diagnosticListener.WriteConnectionOpenError(operationId, this, t.Exception); } else { s_diagnosticListener.WriteConnectionOpenAfter(operationId, this); } }, TaskScheduler.Default); } if (cancellationToken.IsCancellationRequested) { result.SetCanceled(); return(result.Task); } bool completed; try { completed = TryOpen(completion); } catch (Exception e) { s_diagnosticListener.WriteConnectionOpenError(operationId, this, e); result.SetException(e); return(result.Task); } if (completed) { result.SetResult(null); } else { CancellationTokenRegistration registration = new CancellationTokenRegistration(); if (cancellationToken.CanBeCanceled) { registration = cancellationToken.Register(s => ((TaskCompletionSource <DbConnectionInternal>)s).TrySetCanceled(), completion); } OpenAsyncRetry retry = new OpenAsyncRetry(this, completion, result, registration); _currentCompletion = new Tuple <TaskCompletionSource <DbConnectionInternal>, Task>(completion, result.Task); completion.Task.ContinueWith(retry.Retry, TaskScheduler.Default); return(result.Task); } return(result.Task); } catch (Exception ex) { s_diagnosticListener.WriteConnectionOpenError(operationId, this, ex); throw; } finally { SqlStatistics.StopTimer(statistics); } }