/// <summary> /// 处理正常的数据请求 /// </summary> /// <param name="requestContext">请求上下文</param> private void ProcessRequest(RequestContext requestContext) { if (requestContext.Packet.fromClient == false) { FastWebSocketCommon.SetApiActionTaskResult(requestContext, this.TaskSetActionTable); return; } var action = this.GetApiAction(requestContext); if (action == null) { return; } var actionContext = new ActionContext(requestContext, action); var fastApiService = this.GetFastApiService(actionContext); if (fastApiService == null) { return; } // 执行Api行为 fastApiService.Execute(actionContext); // 释放资源 this.DependencyResolver.TerminateService(fastApiService); }
/// <summary> /// 绑定服务 /// </summary> /// <param name="apiServiceType">Api服务类型</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <returns></returns> public FastWebSocketServer BindService(IEnumerable <Type> apiServiceType) { if (apiServiceType == null) { throw new ArgumentNullException("apiServiceType"); } if (apiServiceType.Count() == 0) { throw new ArgumentOutOfRangeException("apiServiceType", "apiServiceType集合不能为空"); } if (apiServiceType.Any(item => item == null)) { throw new ArgumentException("apiServiceType不能含null值"); } if (apiServiceType.Any(item => item.IsInterface || item.IsAbstract)) { throw new ArgumentException("apiServiceType不能是接口或抽象类"); } if (apiServiceType.Any(item => typeof(IFastApiService).IsAssignableFrom(item) == false)) { throw new ArgumentException(string.Format("apiServiceType必须派生于{0}", typeof(IFastApiService).Name)); } foreach (var type in apiServiceType) { var actions = FastWebSocketCommon.GetServiceApiActions(type); this.apiActionList.AddRange(actions); } return(this); }
/// <summary> /// 处理远返回的程异常 /// </summary> /// <param name="requestContext">请求上下文</param> private void ProcessRemoteException(RequestContext requestContext) { var remoteException = FastWebSocketCommon.SetApiActionTaskException(this.TaskSetActionTable, requestContext); if (remoteException != null) { var exceptionContext = new ExceptionContext(requestContext, remoteException); this.ExecGlobalExceptionFilters(exceptionContext); } }
/// <summary> /// 处理Api行为执行过程中产生的异常 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="actionfilters">过滤器</param> /// <param name="exception">异常项</param> private void ProcessExecutingException(ActionContext actionContext, IEnumerable <IFilter> actionfilters, Exception exception) { var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(actionContext, exception)); FastWebSocketCommon.SetRemoteException(this.Server.JsonSerializer, exceptionContext); this.ExecExceptionFilters(actionfilters, exceptionContext); if (exceptionContext.ExceptionHandled == false) { throw exception; } }
/// <summary> /// 获取Api行为 /// </summary> /// <param name="requestContext">请求上下文</param> /// <returns></returns> private ApiAction GetApiAction(RequestContext requestContext) { var action = this.apiActionList.TryGet(requestContext.Packet.api); if (action == null) { var exception = new ApiNotExistException(requestContext.Packet.api); var exceptionContext = new ExceptionContext(requestContext, exception); FastWebSocketCommon.SetRemoteException(this.JsonSerializer, exceptionContext); this.ExecGlobalExceptionFilters(exceptionContext); } return(action); }
/// <summary> /// 处理远返回的程异常 /// </summary> /// <param name="requestContext">请求上下文</param> private void ProcessRemoteException(RequestContext requestContext) { var remoteException = FastWebSocketCommon.SetApiActionTaskException(this.taskSetActionTable, requestContext); if (remoteException == null) { return; } var exceptionContext = new ExceptionContext(requestContext, remoteException); this.ExecExceptionFilters(exceptionContext); if (exceptionContext.ExceptionHandled == false) { throw exceptionContext.Exception; } }
/// <summary> /// 调用自身实现的Api行为 /// 将返回值发送给客户端 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="filters">过滤器</param> /// <exception cref="SerializerException"></exception> private void ExecuteAction(ActionContext actionContext, IEnumerable <IFilter> filters) { // 执行Filter this.ExecFiltersBeforeAction(filters, actionContext); var parameters = FastWebSocketCommon.GetApiActionParameters(actionContext.Session.JsonSerializer, actionContext); var returnValue = actionContext.Action.Execute(this, parameters); // 执行Filter this.ExecFiltersAfterAction(filters, actionContext); // 返回数据 if (actionContext.Action.IsVoidReturn == false && actionContext.Session.IsConnected) { var packet = actionContext.Packet; packet.body = returnValue; var packetJson = actionContext.Session.JsonSerializer.Serialize(packet); actionContext.Session.SendText(packetJson); } }
/// <summary> /// 获取ApiService实例 /// </summary> /// <param name="actionContext">Api行为上下文</param> /// <returns></returns> private IFastApiService GetFastApiService(ActionContext actionContext) { // 获取服务实例 var fastApiService = (IFastApiService)DependencyResolver.Current.GetService(actionContext.Action.DeclaringService); if (fastApiService != null) { return(fastApiService); } var exception = new Exception(string.Format("无法获取类型{0}的实例", actionContext.Action.DeclaringService)); var exceptionContext = new ExceptionContext(actionContext, exception); FastWebSocketCommon.SetRemoteException(this.JsonSerializer, exceptionContext); this.ExecExceptionFilters(exceptionContext); if (exceptionContext.ExceptionHandled == false) { throw exception; } return(null); }
/// <summary> /// 获取ApiService实例 /// </summary> /// <param name="actionContext">Api行为上下文</param> /// <returns></returns> private IFastApiService GetFastApiService(ActionContext actionContext) { IFastApiService fastApiService = null; Exception innerException = null; try { fastApiService = (IFastApiService)this.DependencyResolver.GetService(actionContext.Action.DeclaringService); } catch (Exception ex) { innerException = ex; } if (fastApiService == null) { var exception = new ResolveException(actionContext.Action.DeclaringService, innerException); var exceptionContext = new ExceptionContext(actionContext, exception); FastWebSocketCommon.SetRemoteException(this.JsonSerializer, exceptionContext); this.ExecGlobalExceptionFilters(exceptionContext); } return(fastApiService); }