Esempio n. 1
0
        // 处理外部调用
        private static async Task <string> _process_method_call(string _module_name, string _method_name, string _content)
        {
            string _resp = "";

            // 确认是否能直接调用
            foreach (var(_key, _obj) in Singletons.CallerMap)
            {
                if (!_key.Item1.StartsWith(_module_name))
                {
                    continue;
                }

                // 获取参数内容
                var _method = _key.Item2.GetMethod(_method_name);
                if (_method == null)
                {
                    throw new MissingMethodException($"未在模块 {_module_name} 中找到");
                }
                var       _param_infos = _method.GetParameters();
                object [] _params      = new object [_param_infos?.Length ?? 0];
                if (_content?.Length > 0 && _params.Length > 0)
                {
                    JObject _param_obj = JObject.Parse(_content);
                    for (int i = 0; i < _params.Length; ++i)
                    {
                        _params [i] = _param_obj [_param_infos [i].Name].ToObject(_param_infos [i].ParameterType);
                    }
                }

                // 调用
                try {
                    var _ret = _method.Invoke(_obj, _params);
                    if (_method.ReturnType == typeof(Task))
                    {
                        await(Task) _ret;
                        _resp = JsonConvert.SerializeObject(new { result = "success" });
                    }
                    else
                    {
                        await(Task) _ret;
                        _ret  = _ret.GetType().InvokeMember("Result", BindingFlags.GetProperty, null, _ret, Array.Empty <object> ());
                        _resp = JsonConvert.SerializeObject(new { result = "success", content = _ret });
                    }
                } catch (Exception ex) {
                    _resp = JsonConvert.SerializeObject(new { result = "failure", reason = ex.Message });
                }
                return(_resp);
            }

            // 转发调用
            try {
                return(await Singletons.InvokeRemoteService(_module_name, _method_name, _content));
            } catch (Exception ex) {
                _resp = JsonConvert.SerializeObject(new { result = "failure", reason = ex.Message });
            }
            return(_resp);
        }
Esempio n. 2
0
        // 调用远程函数的中转:将远程请求的结果解析并返回给调用者
        private static async Task <object> _invoke_method_impl(string _service_name, string _method_name, Dictionary <string, object> _params, Type _ret_type)
        {
            string _content = JObject.FromObject(_params).ToString();
            var    _ret     = await Singletons.InvokeRemoteService(_service_name, _method_name, _content);

            JObject _o = JObject.Parse(_ret);

            if (_o ["result"].ToObject <string> () == "success")
            {
                if (_ret_type == typeof(void) || _ret_type == typeof(Task))
                {
                    return(null);
                }
                else if (_ret_type?.BaseType == typeof(Task))
                {
                    return(_o ["content"].ToObject(_ret_type.GenericTypeArguments [0]));
                }
                else
                {
                    throw new MethodAccessException("返回类型必须基于Task");
                }
            }
            throw new MethodAccessException(_o ["reason"].ToObject <string> ());
        }