@@ -13,32 +13,22 @@ public static class AspectContextRuntimeExtensions
1313
1414 internal static readonly ConcurrentDictionary < MethodInfo , MethodReflector > reflectorTable = new ConcurrentDictionary < MethodInfo , MethodReflector > ( ) ;
1515
16+ public static Task AwaitIfAsync ( this AspectContext aspectContext )
17+ {
18+ return AwaitIfAsync ( aspectContext , aspectContext . ReturnValue ) ;
19+ }
20+
1621 public static async Task AwaitIfAsync ( this AspectContext aspectContext , object returnValue )
1722 {
1823 if ( returnValue == null )
1924 {
2025 return ;
2126 }
22- if ( returnValue is Task task )
23- {
24- try
25- {
26- await task ;
27- }
28- catch ( Exception ex )
29- {
30- throw aspectContext . InvocationException ( ex ) ;
31- }
32- }
33- }
3427
35- public static AspectInvocationException InvocationException ( this AspectContext aspectContext , Exception exception )
36- {
37- if ( exception is AspectInvocationException aspectInvocationException )
28+ if ( returnValue is Task task )
3829 {
39- return aspectInvocationException ;
30+ await task ;
4031 }
41- return new AspectInvocationException ( aspectContext , exception ) ;
4232 }
4333
4434 public static bool IsAsync ( this AspectContext aspectContext )
@@ -47,33 +37,44 @@ public static bool IsAsync(this AspectContext aspectContext)
4737 {
4838 throw new ArgumentNullException ( nameof ( aspectContext ) ) ;
4939 }
40+
5041 var isAsyncFromMetaData = isAsyncCache . GetOrAdd ( aspectContext . ServiceMethod , IsAsyncFromMetaData ) ;
5142 if ( isAsyncFromMetaData )
5243 {
5344 return true ;
5445 }
46+
5547 if ( aspectContext . ReturnValue != null )
5648 {
5749 return IsAsyncType ( aspectContext . ReturnValue . GetType ( ) . GetTypeInfo ( ) ) ;
5850 }
51+
5952 return false ;
6053 }
6154
55+ public static async Task < T > UnwrapAsyncReturnValue < T > ( this AspectContext aspectContext )
56+ {
57+ return ( T ) await UnwrapAsyncReturnValue ( aspectContext ) ;
58+ }
59+
6260 public static Task < object > UnwrapAsyncReturnValue ( this AspectContext aspectContext )
6361 {
6462 if ( aspectContext == null )
6563 {
6664 throw new ArgumentNullException ( nameof ( aspectContext ) ) ;
6765 }
66+
6867 if ( ! aspectContext . IsAsync ( ) )
6968 {
7069 throw new AspectInvocationException ( aspectContext , new InvalidOperationException ( "This operation only support asynchronous method." ) ) ;
7170 }
71+
7272 var returnValue = aspectContext . ReturnValue ;
7373 if ( returnValue == null )
7474 {
7575 return null ;
7676 }
77+
7778 var returnTypeInfo = returnValue . GetType ( ) . GetTypeInfo ( ) ;
7879 return Unwrap ( returnValue , returnTypeInfo ) ;
7980 }
@@ -90,7 +91,7 @@ private static async Task<object> Unwrap(object value, TypeInfo valueTypeInfo)
9091 else if ( valueTypeInfo . IsValueTask ( ) )
9192 {
9293 // Is there better solution to unwrap ?
93- result = ( object ) ( await ( dynamic ) value ) ;
94+ result = ( object ) ( await ( dynamic ) value ) ;
9495 }
9596 else if ( value is Task )
9697 {
@@ -111,6 +112,7 @@ private static async Task<object> Unwrap(object value, TypeInfo valueTypeInfo)
111112 {
112113 return Unwrap ( result , resultTypeInfo ) ;
113114 }
115+
114116 return result ;
115117 }
116118
@@ -120,13 +122,15 @@ private static bool IsAsyncFromMetaData(MethodInfo method)
120122 {
121123 return true ;
122124 }
125+
123126 if ( method . IsDefined ( typeof ( AsyncAspectAttribute ) , true ) )
124127 {
125128 if ( method . ReturnType == typeof ( object ) )
126129 {
127130 return true ;
128131 }
129132 }
133+
130134 return false ;
131135 }
132136
@@ -137,14 +141,17 @@ private static bool IsAsyncType(TypeInfo typeInfo)
137141 {
138142 return true ;
139143 }
144+
140145 if ( typeInfo . IsTaskWithResult ( ) )
141146 {
142147 return true ;
143148 }
149+
144150 if ( typeInfo . IsValueTask ( ) )
145151 {
146152 return true ;
147153 }
154+
148155 return false ;
149156 }
150157 }
0 commit comments