@@ -103,43 +103,35 @@ public object Around(
103103
104104 if ( result is Task task )
105105 {
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+
116112 // Only handle response if it's not a void Task
117113 if ( task . GetType ( ) . IsGenericType )
118114 {
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 ) ;
128117 }
129-
130118 _xRayRecorder . EndSubsegment ( ) ;
131119 return task ;
132120 }
133121
134122 HandleResponse ( metadataName , result , trigger . CaptureMode , @namespace ) ;
123+
135124 _xRayRecorder . EndSubsegment ( ) ;
136125 return result ;
137126 }
138127 catch ( Exception ex )
139128 {
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 ( ) ;
143135 throw ;
144136 }
145137 finally
@@ -171,7 +163,7 @@ private void BeginSegment(string segmentName, string @namespace)
171163 private void HandleResponse ( string name , object result , TracingCaptureMode captureMode , string @namespace )
172164 {
173165 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
175167
176168 // Skip if the result is VoidTaskResult
177169 if ( result . GetType ( ) . Name == "VoidTaskResult" ) return ;
0 commit comments