1- using Cyaim . WebSocketServer . Infrastructure . AccessControl ;
1+ using Cyaim . WebSocketServer . Infrastructure . AccessControl ;
22using Cyaim . WebSocketServer . Infrastructure . Configures ;
33using Cyaim . WebSocketServer . Infrastructure . Injectors ;
44using Cyaim . WebSocketServer . Infrastructure . Metrics ;
@@ -1076,53 +1076,33 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
10761076 // Async api support
10771077 if ( invokeResult is Task task )
10781078 {
1079- var taskType = task . GetType ( ) ;
1080- bool isGenericTask = taskType . IsGenericType && taskType . GetGenericTypeDefinition ( ) == typeof ( Task < > ) ;
1079+ await Task . WhenAny ( task , Task . Delay ( Timeout . Infinite , appLifetime . ApplicationStopping ) ) ;
10811080
1082- // 预先准备好反射所需的 PropertyInfo(如果是 Task<T>)
1083- PropertyInfo resultProperty = null ;
1084- if ( isGenericTask ) resultProperty = taskType . GetProperty ( "Result" ) ;
1085-
1086- try
1087- {
1088- // 等待任务完成(异步操作,不阻塞线程)
1089- await task . ConfigureAwait ( false ) ;
1090- }
1091- catch ( Exception ex )
1092- {
1093- // await 会抛出 AggregateException,提取内部异常
1094- if ( ex is AggregateException aggEx && aggEx . InnerException != null )
1095- {
1096- ex = aggEx . InnerException ;
1097- }
1098- mvcResponse = await webSocketOptions . OnException ( ex , request , mvcResponse , context , webSocketOptions , context . Request . Path , logger ) . ConfigureAwait ( false ) ;
1099- }
1100-
1101- // 检查任务状态(await 后任务已完成,但需要检查是否有异常或取消)
1102- if ( task . IsFaulted && task . Exception != null )
1081+ if ( task . IsCanceled || task . IsFaulted )
11031082 {
1104- // 抛出内部异常(AggregateException 的第一个内部异常)
1105- var innerException = task . Exception . InnerException ?? task . Exception ;
1106- mvcResponse = await webSocketOptions . OnException ( innerException , request , mvcResponse , context , webSocketOptions , context . Request . Path , logger ) . ConfigureAwait ( false ) ;
1083+ await task ;
11071084 }
11081085
1109- if ( task . IsCanceled )
1086+ if ( task . Exception != null )
11101087 {
1111- mvcResponse = await webSocketOptions . OnException ( new TaskCanceledException ( task ) , request , mvcResponse , context , webSocketOptions , context . Request . Path , logger ) . ConfigureAwait ( false ) ;
1088+ throw task . Exception ;
11121089 }
11131090
1114- // 检查是否是 Task<T> 类型,需要获取返回值
1115- if ( isGenericTask && resultProperty != null )
1091+ if ( method . ReturnType == typeof ( Task ) )
11161092 {
1117- // 使用预先准备好的 PropertyInfo 获取结果(此时任务已完成,不会阻塞)
1118- invokeResult = resultProperty . GetValue ( task ) ;
1093+ invokeResult = null ;
11191094 }
11201095 else
11211096 {
1122- invokeResult = null ;
1097+ Func < Task , object > taskResultGetter = null ;
1098+ webSocketOptions . WatchAssemblyContext . MethodTaskResultGetters ? . TryGetValue ( method , out taskResultGetter ) ;
1099+ invokeResult = taskResultGetter != null
1100+ ? taskResultGetter ( task )
1101+ : null ;
11231102 }
11241103 }
11251104
1105+
11261106 #endregion
11271107
11281108
@@ -1137,7 +1117,11 @@ public static async Task<MvcResponseScheme> MvcDistributeAsync(WebSocketRouteOpt
11371117 {
11381118 MvcResponseScheme resp = new MvcResponseScheme ( ) { Id = request . Id , Status = 1 , Target = request . Target , RequestTime = requestTime , CompleteTime = DateTime . Now . Ticks } ;
11391119
1140- ex = ( ex . InnerException ?? ex ) ;
1120+ if ( ex is AggregateException aggEx && aggEx . InnerException != null )
1121+ {
1122+ ex = aggEx . InnerException ;
1123+ }
1124+
11411125 resp . Msg = string . Format ( I18nText . WS_INTERACTIVE_TEXT_TEMPALTE , context . Connection . RemoteIpAddress , context . Connection . RemotePort , context . Connection . Id , I18nText . MvcDistributeAsync_Target + requestPath + Environment . NewLine + ex . Message + Environment . NewLine + ex . StackTrace ) ;
11421126 logger . LogInformation ( resp . Msg ) ;
11431127
0 commit comments