@@ -1064,42 +1064,49 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
10641064 // Async api support
10651065 if ( invokeResult is Task task )
10661066 {
1067- // 检查是否是 Task<T> 类型
10681067 var taskType = task . GetType ( ) ;
1069- if ( taskType . IsGenericType && taskType . GetGenericTypeDefinition ( ) == typeof ( Task < > ) )
1070- {
1071- // 这是 Task<T>,需要获取返回值
1072- // 先 await 任务完成,避免同步阻塞
1073- await task . ConfigureAwait ( false ) ;
1068+ bool isGenericTask = taskType . IsGenericType && taskType . GetGenericTypeDefinition ( ) == typeof ( Task < > ) ;
10741069
1075- // 检查任务是否有异常
1076- if ( task . IsFaulted && task . Exception != null )
1077- {
1078- // 抛出内部异常(AggregateException 的第一个内部异常)
1079- var innerException = task . Exception . InnerException ?? task . Exception ;
1080- throw innerException ;
1081- }
1070+ // 预先准备好反射所需的 PropertyInfo(如果是 Task<T>)
1071+ PropertyInfo resultProperty = null ;
1072+ if ( isGenericTask ) resultProperty = taskType . GetProperty ( "Result" ) ;
10821073
1083- // 使用反射获取 Result 属性值(此时任务已完成,不会阻塞)
1084- var resultProperty = taskType . GetProperty ( "Result" ) ;
1085- if ( resultProperty != null )
1074+ try
1075+ {
1076+ // 等待任务完成(异步操作,不阻塞线程)
1077+ await task . ConfigureAwait ( false ) ;
1078+ }
1079+ catch ( Exception ex )
1080+ {
1081+ // await 会抛出 AggregateException,提取内部异常
1082+ if ( ex is AggregateException aggEx && aggEx . InnerException != null )
10861083 {
1087- invokeResult = resultProperty . GetValue ( task ) ;
1084+ throw aggEx . InnerException ;
10881085 }
1086+ throw ;
10891087 }
1090- else
1088+
1089+ // 检查任务状态(await 后任务已完成,但需要检查是否有异常或取消)
1090+ if ( task . IsFaulted && task . Exception != null )
10911091 {
1092- // 这是 Task(无返回值),直接 await
1093- await task . ConfigureAwait ( false ) ;
1092+ // 抛出内部异常(AggregateException 的第一个内部异常)
1093+ var innerException = task . Exception . InnerException ?? task . Exception ;
1094+ throw innerException ;
1095+ }
10941096
1095- // 检查任务是否有异常
1096- if ( task . IsFaulted && task . Exception != null )
1097- {
1098- // 抛出内部异常(AggregateException 的第一个内部异常)
1099- var innerException = task . Exception . InnerException ?? task . Exception ;
1100- throw innerException ;
1101- }
1097+ if ( task . IsCanceled )
1098+ {
1099+ throw new TaskCanceledException ( task ) ;
1100+ }
11021101
1102+ // 检查是否是 Task<T> 类型,需要获取返回值
1103+ if ( isGenericTask && resultProperty != null )
1104+ {
1105+ // 使用预先准备好的 PropertyInfo 获取结果(此时任务已完成,不会阻塞)
1106+ invokeResult = resultProperty . GetValue ( task ) ;
1107+ }
1108+ else
1109+ {
11031110 invokeResult = null ;
11041111 }
11051112 }
@@ -1127,9 +1134,6 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
11271134
11281135 MvcResponseScheme customResp = await webSocketOptions . OnException ( ex , request , resp , context , webSocketOptions , context . Request . Path , logger ) . ConfigureAwait ( false ) ;
11291136
1130- //if (!webSocketOptions.IsDevelopment)
1131- // resp.Msg = null;
1132-
11331137 return customResp ;
11341138 }
11351139
0 commit comments