Exemple #1
0
        /// <summary>
        /// Invokes the specified service name.
        /// </summary>
        /// <param name="serviceName">Name of the service.</param>
        /// <param name="jsonInput">The json input.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static string Invoke(string serviceName, string jsonInput)
        {
            var serviceType = Find(serviceName);

            if (serviceType == null)
            {
                throw new InvalidOperationException();
            }

            var service    = ServiceFactory.Create(serviceType);
            var properties = serviceType.GetProperties();

            SetInputProperties(service, jsonInput, properties);

            var res = new DynamicJsonModel();

            try
            {
                //调用服务
                service.Invoke();
            }
            catch (Exception ex)
            {
                res.SetProperty(ClientResult.SuccessProperty, false);
                res.SetProperty(ClientResult.MessageProperty, ex.Message);
                return(res.ToJsonString());
            }

            //结果输出
            ReadOutputProperties(service, properties, res);

            return(res.ToJsonString());
        }
Exemple #2
0
        /// <summary>
        /// 结果输出
        /// </summary>
        /// <param name="service"></param>
        /// <param name="properties"></param>
        /// <param name="res"></param>
        private static void ReadOutputProperties(IService service, System.Reflection.PropertyInfo[] properties, DynamicJsonModel res)
        {
            foreach (var property in properties)
            {
                if (ServiceHelper.IsOutput(property))
                {
                    var value = property.GetValue(service, null);

                    //如果是 FlowService.Result ,则使用小写直接把 Result 输出。
                    //这里的判断不能直接使用 property == FlowServiceResultName,这是因为它们是两个不同的运行时对象。
                    if (property.DeclaringType == FlowServiceResultName.DeclaringType && property.Name == FlowServiceResultName.Name)
                    {
                        var result = (Result)value;
                        res.SetProperty(ClientResult.SuccessProperty, BooleanBoxes.Box(result.Success));
                        res.SetProperty(ClientResult.MessageProperty, result.Message);
                        continue;
                    }

                    //进行格式转换。
                    value = ConvertOutputComponent(value);

                    res.SetProperty(property.Name, value);
                }
            }
        }