@@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
114
114
private static bool _forceInternalEndQuery = false ;
115
115
#endif
116
116
117
- private static readonly SqlDiagnosticListener _diagnosticListener = new SqlDiagnosticListener ( SqlClientDiagnosticListenerExtensions . DiagnosticListenerName ) ;
117
+ private static readonly SqlDiagnosticListener s_diagnosticListener = new SqlDiagnosticListener ( SqlClientDiagnosticListenerExtensions . DiagnosticListenerName ) ;
118
118
private bool _parentOperationStarted = false ;
119
119
120
120
internal static readonly Action < object > s_cancelIgnoreFailure = CancelIgnoreFailureCallback ;
@@ -554,7 +554,7 @@ internal SqlStatistics Statistics
554
554
if ( null != _activeConnection )
555
555
{
556
556
if ( _activeConnection . StatisticsEnabled ||
557
- _diagnosticListener . IsEnabled ( SqlClientDiagnosticListenerExtensions . SqlAfterExecuteCommand ) )
557
+ s_diagnosticListener . IsEnabled ( SqlClientDiagnosticListenerExtensions . SqlAfterExecuteCommand ) )
558
558
{
559
559
return _activeConnection . Statistics ;
560
560
}
@@ -1082,7 +1082,7 @@ public override object ExecuteScalar()
1082
1082
// between entry into Execute* API and the thread obtaining the stateObject.
1083
1083
_pendingCancel = false ;
1084
1084
1085
- using ( DiagnosticScope diagnosticScope = _diagnosticListener . CreateCommandScope ( this , _transaction ) )
1085
+ using ( DiagnosticScope diagnosticScope = s_diagnosticListener . CreateCommandScope ( this , _transaction ) )
1086
1086
using ( TryEventScope . Create ( "SqlCommand.ExecuteScalar | API | ObjectId {0}" , ObjectID ) )
1087
1087
{
1088
1088
SqlStatistics statistics = null ;
@@ -1162,7 +1162,7 @@ public override int ExecuteNonQuery()
1162
1162
// between entry into Execute* API and the thread obtaining the stateObject.
1163
1163
_pendingCancel = false ;
1164
1164
1165
- using ( var diagnosticScope = _diagnosticListener . CreateCommandScope ( this , _transaction ) )
1165
+ using ( var diagnosticScope = s_diagnosticListener . CreateCommandScope ( this , _transaction ) )
1166
1166
using ( TryEventScope . Create ( "SqlCommand.ExecuteNonQuery | API | Object Id {0}" , ObjectID ) )
1167
1167
{
1168
1168
SqlStatistics statistics = null ;
@@ -1670,7 +1670,7 @@ public XmlReader ExecuteXmlReader()
1670
1670
// between entry into Execute* API and the thread obtaining the stateObject.
1671
1671
_pendingCancel = false ;
1672
1672
1673
- using ( DiagnosticScope diagnosticScope = _diagnosticListener . CreateCommandScope ( this , _transaction ) )
1673
+ using ( DiagnosticScope diagnosticScope = s_diagnosticListener . CreateCommandScope ( this , _transaction ) )
1674
1674
using ( TryEventScope . Create ( "SqlCommand.ExecuteXmlReader | API | Object Id {0}" , ObjectID ) )
1675
1675
{
1676
1676
SqlStatistics statistics = null ;
@@ -2002,7 +2002,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
2002
2002
bool success = false ;
2003
2003
int ? sqlExceptionNumber = null ;
2004
2004
Exception e = null ;
2005
- Guid operationId = _diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2005
+ Guid operationId = s_diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2006
2006
2007
2007
using ( TryEventScope . Create ( "SqlCommand.ExecuteReader | API | Object Id {0}" , ObjectID ) )
2008
2008
{
@@ -2030,11 +2030,11 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
2030
2030
WriteEndExecuteEvent ( success , sqlExceptionNumber , synchronous : true ) ;
2031
2031
if ( e != null )
2032
2032
{
2033
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2033
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2034
2034
}
2035
2035
else
2036
2036
{
2037
- _diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2037
+ s_diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2038
2038
}
2039
2039
}
2040
2040
}
@@ -2129,12 +2129,16 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
2129
2129
Exception e = task . Exception . InnerException ;
2130
2130
if ( ! _parentOperationStarted )
2131
2131
{
2132
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2132
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2133
2133
}
2134
2134
source . SetException ( e ) ;
2135
2135
}
2136
2136
else
2137
2137
{
2138
+ if ( ! _parentOperationStarted )
2139
+ {
2140
+ s_diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2141
+ }
2138
2142
if ( task . IsCanceled )
2139
2143
{
2140
2144
source . SetCanceled ( ) ;
@@ -2143,10 +2147,6 @@ private void CleanupExecuteReaderAsync(Task<SqlDataReader> task, TaskCompletionS
2143
2147
{
2144
2148
source . SetResult ( task . Result ) ;
2145
2149
}
2146
- if ( ! _parentOperationStarted )
2147
- {
2148
- _diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2149
- }
2150
2150
}
2151
2151
}
2152
2152
@@ -2524,7 +2524,7 @@ private Task<int> InternalExecuteNonQueryWithRetryAsync(CancellationToken cancel
2524
2524
private Task < int > InternalExecuteNonQueryAsync ( CancellationToken cancellationToken )
2525
2525
{
2526
2526
SqlClientEventSource . Log . TryCorrelationTraceEvent ( "SqlCommand.InternalExecuteNonQueryAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'" , ObjectID , ActivityCorrelator . Current , Connection ? . ClientConnectionId , CommandText ) ;
2527
- Guid operationId = _diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2527
+ Guid operationId = s_diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2528
2528
2529
2529
TaskCompletionSource < int > source = new TaskCompletionSource < int > ( ) ;
2530
2530
@@ -2544,32 +2544,35 @@ private Task<int> InternalExecuteNonQueryAsync(CancellationToken cancellationTok
2544
2544
{
2545
2545
returnedTask = RegisterForConnectionCloseNotification ( returnedTask ) ;
2546
2546
2547
- Task < int > . Factory . FromAsync ( BeginExecuteNonQueryAsync , EndExecuteNonQueryAsync , null ) . ContinueWith ( ( t ) =>
2548
- {
2549
- registration . Dispose ( ) ;
2550
- if ( t . IsFaulted )
2551
- {
2552
- Exception e = t . Exception . InnerException ;
2553
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2554
- source . SetException ( e ) ;
2555
- }
2556
- else
2547
+ Task < int > . Factory . FromAsync ( BeginExecuteNonQueryAsync , EndExecuteNonQueryAsync , null )
2548
+ . ContinueWith ( ( Task < int > task ) =>
2557
2549
{
2558
- if ( t . IsCanceled )
2550
+ registration . Dispose ( ) ;
2551
+ if ( task . IsFaulted )
2559
2552
{
2560
- source . SetCanceled ( ) ;
2553
+ Exception e = task . Exception . InnerException ;
2554
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2555
+ source . SetException ( e ) ;
2561
2556
}
2562
2557
else
2563
2558
{
2564
- source . SetResult ( t . Result ) ;
2559
+ s_diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2560
+ if ( task . IsCanceled )
2561
+ {
2562
+ source . SetCanceled ( ) ;
2563
+ }
2564
+ else
2565
+ {
2566
+ source . SetResult ( task . Result ) ;
2567
+ }
2565
2568
}
2566
- _diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2567
- }
2568
- } , TaskScheduler . Default ) ;
2569
+ } ,
2570
+ TaskScheduler . Default
2571
+ ) ;
2569
2572
}
2570
2573
catch ( Exception e )
2571
2574
{
2572
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2575
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2573
2576
source . SetException ( e ) ;
2574
2577
}
2575
2578
@@ -2628,7 +2631,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
2628
2631
Guid operationId = default ( Guid ) ;
2629
2632
if ( ! _parentOperationStarted )
2630
2633
{
2631
- operationId = _diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2634
+ operationId = s_diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2632
2635
}
2633
2636
2634
2637
TaskCompletionSource < SqlDataReader > source = new TaskCompletionSource < SqlDataReader > ( ) ;
@@ -2673,7 +2676,7 @@ private Task<SqlDataReader> InternalExecuteReaderAsync(CommandBehavior behavior,
2673
2676
{
2674
2677
if ( ! _parentOperationStarted )
2675
2678
{
2676
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2679
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2677
2680
}
2678
2681
2679
2682
source . SetException ( e ) ;
@@ -2700,7 +2703,7 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
2700
2703
SqlClientEventSource . Log . TryCorrelationTraceEvent ( "SqlCommand.InternalExecuteScalarAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'" , ObjectID , ActivityCorrelator . Current , Connection ? . ClientConnectionId , CommandText ) ;
2701
2704
SqlClientEventSource . Log . TryTraceEvent ( "SqlCommand.InternalExecuteScalarAsync | API> {0}, Client Connection Id {1}, Command Text = '{2}'" , ObjectID , Connection ? . ClientConnectionId , CommandText ) ;
2702
2705
_parentOperationStarted = true ;
2703
- Guid operationId = _diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2706
+ Guid operationId = s_diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2704
2707
2705
2708
return ExecuteReaderAsync ( cancellationToken ) . ContinueWith ( ( executeTask ) =>
2706
2709
{
@@ -2711,68 +2714,71 @@ private Task<object> InternalExecuteScalarAsync(CancellationToken cancellationTo
2711
2714
}
2712
2715
else if ( executeTask . IsFaulted )
2713
2716
{
2714
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , executeTask . Exception . InnerException ) ;
2717
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , executeTask . Exception . InnerException ) ;
2715
2718
source . SetException ( executeTask . Exception . InnerException ) ;
2716
2719
}
2717
2720
else
2718
2721
{
2719
2722
SqlDataReader reader = executeTask . Result ;
2720
- reader . ReadAsync ( cancellationToken ) . ContinueWith ( ( readTask ) =>
2721
- {
2722
- try
2723
+ reader . ReadAsync ( cancellationToken )
2724
+ . ContinueWith ( ( Task < bool > readTask ) =>
2723
2725
{
2724
- if ( readTask . IsCanceled )
2725
- {
2726
- reader . Dispose ( ) ;
2727
- source . SetCanceled ( ) ;
2728
- }
2729
- else if ( readTask . IsFaulted )
2730
- {
2731
- reader . Dispose ( ) ;
2732
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , readTask . Exception . InnerException ) ;
2733
- source . SetException ( readTask . Exception . InnerException ) ;
2734
- }
2735
- else
2726
+ try
2736
2727
{
2737
- Exception exception = null ;
2738
- object result = null ;
2739
- try
2740
- {
2741
- bool more = readTask . Result ;
2742
- if ( more && reader . FieldCount > 0 )
2743
- {
2744
- try
2745
- {
2746
- result = reader . GetValue ( 0 ) ;
2747
- }
2748
- catch ( Exception e )
2749
- {
2750
- exception = e ;
2751
- }
2752
- }
2753
- }
2754
- finally
2728
+ if ( readTask . IsCanceled )
2755
2729
{
2756
2730
reader . Dispose ( ) ;
2731
+ source . SetCanceled ( ) ;
2757
2732
}
2758
- if ( exception != null )
2733
+ else if ( readTask . IsFaulted )
2759
2734
{
2760
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , exception ) ;
2761
- source . SetException ( exception ) ;
2735
+ reader . Dispose ( ) ;
2736
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , readTask . Exception . InnerException ) ;
2737
+ source . SetException ( readTask . Exception . InnerException ) ;
2762
2738
}
2763
2739
else
2764
2740
{
2765
- _diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2766
- source . SetResult ( result ) ;
2741
+ Exception exception = null ;
2742
+ object result = null ;
2743
+ try
2744
+ {
2745
+ bool more = readTask . Result ;
2746
+ if ( more && reader . FieldCount > 0 )
2747
+ {
2748
+ try
2749
+ {
2750
+ result = reader . GetValue ( 0 ) ;
2751
+ }
2752
+ catch ( Exception e )
2753
+ {
2754
+ exception = e ;
2755
+ }
2756
+ }
2757
+ }
2758
+ finally
2759
+ {
2760
+ reader . Dispose ( ) ;
2761
+ }
2762
+ if ( exception != null )
2763
+ {
2764
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , exception ) ;
2765
+ source . SetException ( exception ) ;
2766
+ }
2767
+ else
2768
+ {
2769
+ s_diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2770
+ source . SetResult ( result ) ;
2771
+ }
2767
2772
}
2768
2773
}
2769
- }
2770
- catch ( Exception e )
2771
- {
2772
- // exception thrown by Dispose...
2773
- source . SetException ( e ) ;
2774
- }
2775
- } , TaskScheduler . Default ) ;
2774
+ catch ( Exception e )
2775
+ {
2776
+ // exception thrown by Dispose...
2777
+ source . SetException ( e ) ;
2778
+ }
2779
+ } ,
2780
+ TaskScheduler . Default
2781
+ ) ;
2776
2782
}
2777
2783
_parentOperationStarted = false ;
2778
2784
return source . Task ;
@@ -2797,7 +2803,7 @@ private Task<XmlReader> InternalExecuteXmlReaderWithRetryAsync(CancellationToken
2797
2803
private Task < XmlReader > InternalExecuteXmlReaderAsync ( CancellationToken cancellationToken )
2798
2804
{
2799
2805
SqlClientEventSource . Log . TryCorrelationTraceEvent ( "SqlCommand.InternalExecuteXmlReaderAsync | API | Correlation | Object Id {0}, Activity Id {1}, Client Connection Id {2}, Command Text '{3}'" , ObjectID , ActivityCorrelator . Current , Connection ? . ClientConnectionId , CommandText ) ;
2800
- Guid operationId = _diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2806
+ Guid operationId = s_diagnosticListener . WriteCommandBefore ( this , _transaction ) ;
2801
2807
2802
2808
TaskCompletionSource < XmlReader > source = new TaskCompletionSource < XmlReader > ( ) ;
2803
2809
@@ -2817,32 +2823,36 @@ private Task<XmlReader> InternalExecuteXmlReaderAsync(CancellationToken cancella
2817
2823
{
2818
2824
returnedTask = RegisterForConnectionCloseNotification ( returnedTask ) ;
2819
2825
2820
- Task < XmlReader > . Factory . FromAsync ( BeginExecuteXmlReaderAsync , EndExecuteXmlReaderAsync , null ) . ContinueWith ( ( t ) =>
2821
- {
2822
- registration . Dispose ( ) ;
2823
- if ( t . IsFaulted )
2826
+ Task < XmlReader > . Factory . FromAsync ( BeginExecuteXmlReaderAsync , EndExecuteXmlReaderAsync , null )
2827
+ . ContinueWith ( ( Task < XmlReader > task ) =>
2824
2828
{
2825
- Exception e = t . Exception . InnerException ;
2826
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2827
- source . SetException ( e ) ;
2828
- }
2829
- else
2830
- {
2831
- if ( t . IsCanceled )
2829
+ registration . Dispose ( ) ;
2830
+ if ( task . IsFaulted )
2832
2831
{
2833
- source . SetCanceled ( ) ;
2832
+ Exception e = task . Exception . InnerException ;
2833
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2834
+ source . SetException ( e ) ;
2834
2835
}
2835
2836
else
2836
2837
{
2837
- source . SetResult ( t . Result ) ;
2838
+ s_diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2839
+ if ( task . IsCanceled )
2840
+ {
2841
+ source . SetCanceled ( ) ;
2842
+ }
2843
+ else
2844
+ {
2845
+ source . SetResult ( task . Result ) ;
2846
+ }
2847
+
2838
2848
}
2839
- _diagnosticListener . WriteCommandAfter ( operationId , this , _transaction ) ;
2840
- }
2841
- } , TaskScheduler . Default ) ;
2849
+ } ,
2850
+ TaskScheduler . Default
2851
+ ) ;
2842
2852
}
2843
2853
catch ( Exception e )
2844
2854
{
2845
- _diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2855
+ s_diagnosticListener . WriteCommandError ( operationId , this , _transaction , e ) ;
2846
2856
source . SetException ( e ) ;
2847
2857
}
2848
2858
0 commit comments