private void ExecCall(CmdMethodInfo cmdMethodInfo, ITcpClientAsync client, MsgData msg) { var session = client.UserState as Session; this.OnCmdExecutingEvent(session, msg); if (cmdMethodInfo != null && client != null) { ActionResult result = null; using (var controller = Activator.CreateInstance(cmdMethodInfo.Type) as Controller) { try { controller.Init(session, msg); if (this.OnAuth(cmdMethodInfo, session, msg, out result)) { controller.OnExecuting(); object[] parameters = null; if (cmdMethodInfo.ParameterType != null) { var o = msg.GetData(cmdMethodInfo.ParameterType); parameters = new object[] { o }; } var obj = cmdMethodInfo.Method.Invoke(controller, parameters); if (obj != null && obj is ActionResult) { result = obj as ActionResult; } else if (obj != null && obj is MsgData) { result = new ActionResult(obj as MsgData); } else { result = new ActionResult(); result.SetMsg(MsgStatus.Succeed, null); if (obj != null) { #if NET20 result.Result.SetData(obj); #else var t = cmdMethodInfo.Method.ReturnType; if (t == typeof(System.Threading.Tasks.Task) || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>))) { var task = obj as System.Threading.Tasks.Task; if (task.Status == System.Threading.Tasks.TaskStatus.Created) { task.Start(); } if (!task.IsCompleted || !task.IsCanceled) { task.Wait(); } if (task.IsFaulted && task.Exception != null) { throw task.Exception; } if (t.IsGenericType) { var o = t.GetProperty("Result").GetValue(obj, null); result.Result.SetData(o); } } else { result.Result.SetData(obj); } #endif } } result.Result.Cmd = msg.Cmd; result.Result.Id = msg.Id; controller.OnResult(result); this.OnCmdExecutedEvent(session, msg, result.Result); } } catch (Exception ex) { bool ishand = false; try { ExceptionContext excontext = new ExceptionContext() { Msg = msg, IsHandle = false, Exception = ex }; controller.OnException(excontext); ishand = excontext.IsHandle; result = excontext.Result; } catch (Exception _ex) { this.OnMvcHostServerErrorEvent(_ex); } if (!ishand) { this.OnMvcHostServerErrorEvent(ex); } if (result == null) { result = new ActionResult(); result.SetMsg(MsgStatus.ServerError, "服务器发生错误!"); } } } if (result != null && client.IsConnected) { result.Result.Cmd = msg.Cmd; result.Result.Id = msg.Id; this.SendMsg(result.Result, session); } } }