Beispiel #1
0
        /// <summary>
        /// 根据请求参数执行函数
        /// </summary>
        /// <param name="requestString"></param>
        /// <returns></returns>
        public static Data.ServiceProxyReturn Invoke(DoNet.Common.Data.ServiceProxyParam param, string configpath = "data/proxy.config")
        {
            var result = new Data.ServiceProxyReturn();

            try
            {
                if (param == null)
                {
                    throw new Exception("ServiceProxyParam is null");
                }
                var requestpar = CheckInvoke(param, result, configpath);
                if (requestpar == null)
                {
                    return(result);
                }

                var type = System.Reflection.Assembly.Load(requestpar.Namespace).GetType(requestpar.ClassName);
                if (type == null)
                {
                    throw new Exception("load " + requestpar.ClassName + " faild.");
                }

                System.Reflection.MethodInfo      method     = null;
                System.Reflection.ParameterInfo[] methodpars = null;
                var ms = type.GetMethods();
                foreach (var m in ms)
                {
                    var mpars = m.GetParameters();
                    if (m.Name.Equals(requestpar.MethodName, StringComparison.OrdinalIgnoreCase) && mpars.Length == param.Params.Count)
                    {
                        method     = m;
                        methodpars = mpars;
                        break;
                    }
                }

                if (method == null)
                {
                    throw new Exception("get method " + requestpar.MethodName + " faild.");
                }
                //var methodpars = method.GetParameters();
                var instance = Activator.CreateInstance(type);

                object[] invokepars = null;
                if (methodpars != null && methodpars.Length > 0)
                {
                    invokepars = new object[methodpars.Length];
                    if (methodpars.Length == param.Params.Count)
                    {
                        for (var i = 0; i < invokepars.Length; i++)
                        {
                            if (i >= param.Params.Count || param.Params[i] == null)
                            {
                                continue;
                            }
                            var p = param.Params[i];
                            var t = methodpars[i].ParameterType;//t != typeof(string) &&
                            if ((t.IsClass || t.IsArray || t.IsGenericType))
                            {
                                if (t == typeof(string))
                                {
                                    invokepars[i] = p.Value;
                                }
                                else
                                {
                                    invokepars[i] = p.Value == null ? null : DoNet.Common.Serialization.JSon.JsonToModel(t, p.Value);
                                }
                            }
                            else
                            {
                                invokepars[i] = Convert.ChangeType(p.Value, methodpars[i].ParameterType);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("method " + requestpar.MethodName + " params error.");
                    }
                }

                var re = method.Invoke(instance, invokepars);
                if (re != null)
                {
                    var t = re.GetType();
                    if (t != typeof(string) && (t.IsClass || t.IsArray || t.IsGenericType))
                    {
                        result.Value = DoNet.Common.Serialization.JSon.ModelToJson(re);
                    }
                    else
                    {
                        result.Value = re.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Error = ex.Message;
                result.State = 2;
                DoNet.Common.IO.Logger.Write(ex.ToString());
            }
            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// 检查请求是否正确
 /// </summary>
 /// <param name="par"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static Data.ServiceProxyInvokeItem CheckInvoke(Data.ServiceProxyParam par, Data.ServiceProxyReturn result, string configpath)
 {
     //从配置文件中读取代理的方法配置
     if (methodProxyItems == null)
     {
         var proxyconfigpath = DoNet.Common.IO.PathMg.CheckPath(configpath);
         if (System.IO.File.Exists(proxyconfigpath))
         {
             methodProxyItems = (List <Data.ServiceProxyInvokeItem>)DoNet.Common.Serialization.FormatterHelper.XMLDerObject(typeof(List <Data.ServiceProxyInvokeItem>), proxyconfigpath);
         }
         else
         {
             methodProxyItems = new List <Data.ServiceProxyInvokeItem>();
         }
     }
     foreach (var item in methodProxyItems)
     {
         if (item.Key == par.Key || (item.Namespace == par.Namespace && item.ClassName == par.ClassName && item.MethodName == par.Method))
         {
             var ip = Net.BaseNet.GetWebIPAddress();
             if (item.IPAddress == "*" || item.IPAddress.Contains(ip))
             {
                 return(item);
             }
             else
             {
                 DoNet.Common.IO.Logger.Write(ip + ",没有被允许请求");
                 result.Error = "ip error";
                 result.State = 3;//非法请求
                 return(null);
             }
         }
     }
     result.Error = "异常的请求,请确保请求参数正确。";
     result.State = 3;//非法请求
     return(null);
 }