/// <summary> /// /// </summary> /// <param name="req"></param> /// <param name="operationDesc"></param> /// <returns></returns> protected virtual IServiceResponse OnExecute(IServiceRequest req, IOperationDescriptor operationDesc) { var resp = new ServiceResponse(); //得到领域服务元数据 var serviceDesc = operationDesc.ServiceDescriptor; var svrResolvedContext = new ServiceResolvedContext(serviceDesc, req, resp); ServiceContext.Current = new ServiceContext { Request = req, Response = resp }; try { ListenManager.OnServiceDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; svrResolvedContext.OperationDescriptor = operationDesc; ListenManager.OnOperationDescriptorFound(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; } catch (Exception ex) { resp.AddException(ex); return resp; } object service = null; try { service = ServiceLocator(serviceDesc.Id); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.CreateServiceException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } svrResolvedContext.Service = service; ListenManager.OnServiceResolved(svrResolvedContext); if (svrResolvedContext.Cancelled) return resp; try { req = PopulateModelBinding(operationDesc, req, resp); } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ParameterBindException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } try { if (!ValidateParamters(operationDesc, req, resp)) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } } catch (Exception ex) { resp.AddException(new ServiceDispatcherException(ServiceDispatcherExceptionCode.ModelValidationException, ex) { ServiceName = req.ServiceName, OperationName = req.OperationName }); return resp; } //创建操作上下文对象 var ctx = new OperationExecutedContext(req, operationDesc) { Response = resp }; try { //执行前置过滤器 OnBeforeAction(service, ctx); //如果过滤器进行了必要的拦截则返回 if ((ctx as IOperationExecutingContext).Cancelled) return resp; //调用领域服务方法 resp.Result = operationDesc.Invoke(service, ctx.Arguments.Values.ToArray()); ctx.Service = service; //执行后置过滤器 OnAfterAction(ctx); resp.Success = true; } catch (Exception ex) { resp.AddException(ex); } return resp; }
bool ValidateParamters(IOperationDescriptor operationDesc, IServiceRequest req, ServiceResponse resp) { if (!req.ValidateRequest || req.Arguments == null || req.Arguments.Count == 0) return true; foreach (var key in req.Arguments.Keys) { var errorState = Validator.Validate(req.Arguments[key]); if (errorState.Count > 0) { resp.ErrorState.AddRange(errorState); return false; } } return true; }
private IServiceResponse OnExecute(IServiceRequest req) { IServiceResponse resp = null; IOperationDescriptor operationDesc = null; try { operationDesc = GetOperationDescriptor(req); resp = OnExecute(req, operationDesc); } catch (Exception ex) { resp = new ServiceResponse(ex); } if (req.Context.ContainsKey("RawArguments")) req.Context.Remove("RawArguments"); if (req.Arguments.ContainsKey("AutoCloseServiceContext")) ServiceContext.Current = null; if (resp.Exception != null) { try { ListenManager.OnExceptionFired(req, resp, operationDesc); } finally { } } return resp; }