Ejemplo n.º 1
0
        /// <summary>
        /// 方法调用
        /// </summary>
        /// <param name="kind"></param>
        /// <param name="method"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object Invoke(string kind, string method, NameValueCollection parameters, out Type retType)
        {
            WebOperationContext context  = WebOperationContext.Current;
            BusinessMethodModel metadata = pool.FindMethod(kind, method);

            //返回类型
            retType = metadata.Method.ReturnType;

            //处理请求类型
            if (metadata.HttpMethod == HttpMethod.POST && context.IncomingRequest.Method.ToUpper() == "GET")
            {
                throw new RESTfulException((int)HttpStatusCode.MethodNotAllowed, "Resources can only by the [" + metadata.HttpMethod + "] way to acquire.");
            }

            //实例对象
            object instance = null;

            try
            {
                //调用方法
                object[] arguments = ParameterHelper.Convert(metadata.Parameters, parameters);
                instance = register.Resolve(metadata.Service);
                return(DynamicCalls.GetMethodInvoker(metadata.Method)(instance, arguments));
            }
            finally
            {
                register.Release(instance);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 消息回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void reqService_OnCallback(object sender, ServiceMessageEventArgs args)
        {
            if (args.Result is ResponseMessage)
            {
                var resMsg = args.Result as ResponseMessage;
                base.QueueMessage(resMsg);
            }
            else if (args.Result is CallbackMessage)
            {
                var callbackMsg = args.Result as CallbackMessage;

                if (callback != null)
                {
                    var callbackType = callback.GetType();

                    //获取接口的类型
                    var interfaces = callbackType.GetInterfaces();
                    if (interfaces.Length > 0)
                    {
                        //判断类型是否相同
                        if (interfaces.Any(type => type.FullName == callbackMsg.ServiceName))
                        {
                            var method = CoreHelper.GetMethodFromType(callbackType, callbackMsg.MethodName);

                            //执行委托
                            DynamicCalls.GetMethodInvoker(method).Invoke(callback, callbackMsg.Parameters);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void GetMethodInvokerTest()
        {
            MethodInfo        methodInfo = null; // TODO: 初始化为适当的值
            FastInvokeHandler expected   = null; // TODO: 初始化为适当的值
            FastInvokeHandler actual;

            actual = DynamicCalls.GetMethodInvoker(methodInfo);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 通过字符串的方式来执行方法
        /// </summary>
        /// <param name="obj">方法所在对象</param>
        /// <param name="name">方法名称</param>
        /// <param name="values">方法对应的参数值</param>
        /// <returns></returns>
        protected object RunMethod(object obj, string name, params object[] values)
        {
            Type       type   = obj.GetType();
            MethodInfo method = type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public);

            if (method == null)
            {
                return(null);
            }
            try
            {
                FastInvokeHandler handler = DynamicCalls.GetMethodInvoker(method);
                return(handler.Invoke(obj, values));
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invokeObject"></param>
        /// <param name="MethodName"></param>
        /// <returns></returns>
        private AjaxCallbackParam InvokeMethod(object invokeObject, string MethodName)
        {
            var ajaxMethods = AjaxMethodHelper.GetAjaxMethods(invokeObject.GetType());

            if (ajaxMethods.ContainsKey(MethodName))
            {
                var parameters = ajaxMethods[MethodName].Method.GetParameters();
                var plist      = new List <object>();
                foreach (var p in parameters)
                {
                    object obj = GetObject(p.ParameterType, p.Name);
                    plist.Add(obj);
                }

                var    method  = ajaxMethods[MethodName].Method;
                var    handler = DynamicCalls.GetMethodInvoker(method);
                object value   = handler.Invoke(invokeObject, plist.ToArray());
                return(new AjaxCallbackParam(value));
            }
            else
            {
                throw new AjaxException(string.Format("未找到服务器端方法{0}!", MethodName));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Runs the specified MSG.
        /// </summary>
        /// <param name="reqMsg">The MSG.</param>
        /// <returns>The msg.</returns>
        protected override ResponseMessage Run(RequestMessage reqMsg)
        {
            var resMsg = new ResponseMessage
            {
                TransactionId = reqMsg.TransactionId,
                ReturnType    = reqMsg.ReturnType,
                ServiceName   = reqMsg.ServiceName,
                MethodName    = reqMsg.MethodName
            };

            #region 获取相应的方法

            var methodKey = string.Format("{0}${1}", reqMsg.ServiceName, reqMsg.MethodName);
            if (!hashtable.ContainsKey(methodKey))
            {
                var m = CoreHelper.GetMethodFromType(serviceType, reqMsg.MethodName);
                if (m == null)
                {
                    string message = string.Format("The server【{2}({3})】not find matching method. ({0},{1})."
                                                   , reqMsg.ServiceName, reqMsg.MethodName, DnsHelper.GetHostName(), DnsHelper.GetIPAddress());

                    resMsg.Error = new WarningException(message);
                    return(resMsg);
                }

                hashtable[methodKey] = m;
            }

            #endregion

            //容器实例对象
            object instance = null;

            try
            {
                //定义Method
                var method = hashtable[methodKey] as System.Reflection.MethodInfo;

                //解析服务
                instance = container.Resolve(serviceType);

                //返回拦截服务
                var service = AspectFactory.CreateProxyService(serviceType, instance);

                if (reqMsg.InvokeMethod)
                {
                    var objValue   = reqMsg.Parameters["InvokeParameter"];
                    var jsonString = (objValue == null ? string.Empty : objValue.ToString());

                    //解析参数
                    reqMsg.Parameters = IoCHelper.CreateParameters(method, jsonString);
                }

                //参数赋值
                object[] parameters = IoCHelper.CreateParameterValues(method, reqMsg.Parameters);

                //调用对应的服务
                resMsg.Value = DynamicCalls.GetMethodInvoker(method).Invoke(service, parameters);

                //处理返回参数
                IoCHelper.SetRefParameters(method, resMsg.Parameters, parameters);

                //返回结果数据
                if (reqMsg.InvokeMethod)
                {
                    resMsg.Value = new InvokeData
                    {
                        Value         = SerializationManager.SerializeJson(resMsg.Value),
                        Count         = resMsg.Count,
                        OutParameters = resMsg.Parameters.ToString()
                    };

                    //清除参数集合
                    resMsg.Parameters.Clear();
                }
            }
            catch (Exception ex)
            {
                //捕获全局错误
                resMsg.Error = ex;
            }
            finally
            {
                //释放资源
                container.Release(instance);

                instance = null;
            }

            return(resMsg);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 快速执行Method
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="method"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public static object FastMethodInvoke(object obj, MethodInfo method, params object[] parameters)
 {
     return(DynamicCalls.GetMethodInvoker(method)(obj, parameters));
 }