77using System ;
88using System . Collections . Concurrent ;
99using System . Collections . Generic ;
10+ using System . Runtime . ExceptionServices ;
1011using System . Threading ;
1112using System . Threading . Tasks ;
1213using JetBrains . Annotations ;
@@ -57,7 +58,7 @@ public sealed class Domain : IDisposable, IAsyncDisposable, IHasExtensions, ISes
5758 public event EventHandler Disposing ;
5859
5960 /// <summary>
60- /// Gets the <see cref="Domain"/> of the current <see cref="Session"/>.
61+ /// Gets the <see cref="Domain"/> of the current <see cref="Session"/>.
6162 /// </summary>
6263 /// <seealso cref="Session.Current"/>
6364 /// <seealso cref="Demand"/>
@@ -69,7 +70,7 @@ public static Domain Current {
6970 }
7071
7172 /// <summary>
72- /// Gets the <see cref="Domain"/> of the current <see cref="Session"/>, or throws <see cref="InvalidOperationException"/>,
73+ /// Gets the <see cref="Domain"/> of the current <see cref="Session"/>, or throws <see cref="InvalidOperationException"/>,
7374 /// if active <see cref="Session"/> is not found.
7475 /// </summary>
7576 /// <returns>Current domain.</returns>
@@ -79,7 +80,7 @@ public static Domain Demand()
7980 {
8081 return Session . Demand ( ) . Domain ;
8182 }
82-
83+
8384 /// <summary>
8485 /// Gets the domain configuration.
8586 /// </summary>
@@ -346,19 +347,31 @@ internal async Task<Session> OpenSessionInternalAsync(SessionConfiguration confi
346347 // That would make session accessible for user before
347348 // connection become opened.
348349 session = new Session ( this , storageNode , configuration , false ) ;
350+ ExceptionDispatchInfo exceptionDispatchInfo = null ;
349351 try {
350352 await ( ( SqlSessionHandler ) session . Handler ) . OpenConnectionAsync ( cancellationToken )
351353 . ContinueWith ( t => {
352- if ( sessionScope != null ) {
353- session . AttachToScope ( sessionScope ) ;
354+ if ( t . Status == TaskStatus . RanToCompletion ) {
355+ if ( sessionScope != null ) {
356+ session . AttachToScope ( sessionScope ) ;
357+ }
358+ }
359+ else if ( t . Exception is Exception ex ) {
360+ if ( ex is System . AggregateException aggregateException && aggregateException . InnerExceptions . Count == 1 ) {
361+ ex = aggregateException . InnerExceptions [ 0 ] ;
362+ }
363+ exceptionDispatchInfo = ExceptionDispatchInfo . Capture ( ex ) ;
354364 }
355- } , TaskContinuationOptions . OnlyOnRanToCompletion | TaskContinuationOptions . ExecuteSynchronously )
365+ } , TaskContinuationOptions . ExecuteSynchronously )
356366 . ConfigureAwait ( false ) ;
357367 }
358368 catch ( OperationCanceledException ) {
359369 await session . DisposeSafelyAsync ( ) . ConfigureAwait ( false ) ;
360370 throw ;
361371 }
372+ finally {
373+ exceptionDispatchInfo ? . Throw ( ) ;
374+ }
362375 }
363376 NotifySessionOpen ( session ) ;
364377 return session ;
@@ -370,7 +383,7 @@ internal async Task<Session> OpenSessionInternalAsync(SessionConfiguration confi
370383
371384 /// <inheritdoc/>
372385 public IExtensionCollection Extensions { get ; private set ; }
373-
386+
374387 #endregion
375388
376389 /// <summary>
0 commit comments