@@ -103,43 +103,35 @@ public object Around(
103
103
104
104
if ( result is Task task )
105
105
{
106
- if ( task . IsFaulted && task . Exception != null )
107
- {
108
- var actualException = task . Exception . InnerExceptions . Count == 1
109
- ? task . Exception . InnerExceptions [ 0 ]
110
- : task . Exception ;
111
-
112
- // Capture and rethrow the original exception preserving the stack trace
113
- ExceptionDispatchInfo . Capture ( actualException ) . Throw ( ) ;
114
- }
115
-
106
+ if ( task . IsFaulted )
107
+ {
108
+ // Force the exception to be thrown
109
+ task . Exception ? . Handle ( ex => false ) ;
110
+ }
111
+
116
112
// Only handle response if it's not a void Task
117
113
if ( task . GetType ( ) . IsGenericType )
118
114
{
119
- var taskType = task . GetType ( ) ;
120
- var resultProperty = taskType . GetProperty ( "Result" ) ;
121
-
122
- // Handle the response only if task completed successfully
123
- if ( task . Status == TaskStatus . RanToCompletion )
124
- {
125
- var taskResult = resultProperty ? . GetValue ( task ) ;
126
- HandleResponse ( metadataName , taskResult , trigger . CaptureMode , @namespace ) ;
127
- }
115
+ var taskResult = task . GetType ( ) . GetProperty ( "Result" ) ? . GetValue ( task ) ;
116
+ HandleResponse ( metadataName , taskResult , trigger . CaptureMode , @namespace ) ;
128
117
}
129
-
130
118
_xRayRecorder . EndSubsegment ( ) ;
131
119
return task ;
132
120
}
133
121
134
122
HandleResponse ( metadataName , result , trigger . CaptureMode , @namespace ) ;
123
+
135
124
_xRayRecorder . EndSubsegment ( ) ;
136
125
return result ;
137
126
}
138
127
catch ( Exception ex )
139
128
{
140
- var actualException = ex is AggregateException ae ? ae . InnerException ! : ex ;
141
- HandleException ( actualException , metadataName , trigger . CaptureMode , @namespace ) ;
142
- _xRayRecorder . EndSubsegment ( ) ;
129
+ var actualException = ex is AggregateException ae ? ae . InnerException ! : ex ;
130
+ HandleException ( actualException , metadataName , trigger . CaptureMode , @namespace ) ;
131
+ _xRayRecorder . EndSubsegment ( ) ;
132
+
133
+ // Capture and rethrow the original exception preserving the stack trace
134
+ ExceptionDispatchInfo . Capture ( actualException ) . Throw ( ) ;
143
135
throw ;
144
136
}
145
137
finally
@@ -171,7 +163,7 @@ private void BeginSegment(string segmentName, string @namespace)
171
163
private void HandleResponse ( string name , object result , TracingCaptureMode captureMode , string @namespace )
172
164
{
173
165
if ( ! CaptureResponse ( captureMode ) ) return ;
174
- if ( result == null ) return ; // Don't try to serialize null results
166
+ if ( result == null ) return ; // Don't try to serialize null results
175
167
176
168
// Skip if the result is VoidTaskResult
177
169
if ( result . GetType ( ) . Name == "VoidTaskResult" ) return ;
0 commit comments