Exemple #1
0
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="dllName"></param>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public static RedisSetting GetSetting(string dllName = StaticString.DefaultRedisConfigDll, string sectionName = StaticString.DefautlRedisConfigSection)
        {
            if (!AppConfig.IsExists(dllName))
            {
                throw new NotFoundException("未找到对应的配置文件");
            }
            dllName = Assembly.GetCallingAssembly().GetName().Name;
            if (!AppConfig.DllConfigs[dllName].IsExists(sectionName))
            {
                throw new NotFoundException("未找到对应的配置文件下的配置节点");
            }
            var Setting = new RedisSetting();

            try
            {
                Setting.ClientName        = StringUtil.FormatKey(dllName, sectionName);
                Setting.LocalCacheEnable  = CoralConvert.Convert <bool>(AppConfig.DllConfigs[dllName][sectionName]["LocalCacheEnable"]);
                Setting.LocalCacheTimeout = CoralConvert.Convert <int>(AppConfig.DllConfigs[dllName][sectionName]["LocalCacheTimeout"]);
                Setting.ServerList        = AppConfig.DllConfigs[dllName][sectionName]["ServerList"];
                Setting.Version           = AppConfig.DllConfigs[dllName][sectionName]["Version"];
                Setting.DefaultDb         = Convert.ToInt32(AppConfig.DllConfigs[dllName][sectionName]["DefaultDb"] ?? "0");
                Setting.Password          = AppConfig.DllConfigs[dllName][sectionName]["Password"];
                return(Setting);
            }
            catch (Exception ex)
            {
                throw CoralException.ThrowException(item => item.ConfigError, innerException: ex, param: Setting);
            }
        }
Exemple #2
0
        public virtual void Init(HttpApplication context)
        {
            PlatformContext.Current = new PlatformContext("Caad", CacheFactory.GetRedisCache(sectionName: StaticString.PlatformContextString));
            var app = new AppModel
            {
                Id     = CoralConvert.Convert <long>(AppConfig.AppSettings["AppId"]),
                Key    = AppConfig.AppSettings["AppKey"],
                Secret = AppConfig.AppSettings["AppSecret"],
                Name   = AppConfig.AppSettings["AppName"],
                Code   = AppConfig.AppSettings["AppCode"],
                Domain = AppConfig.DllConfigs["Host"]["Servers"]["AppDomain"]
            };

            if (!string.IsNullOrEmpty(app.Domain))
            {
                PlatformContext.Current.CreateAppContext(app);
            }
            context.BeginRequest += (obj, arg) =>
            {
                if (!string.IsNullOrEmpty(app.Domain))
                {
                    return;
                }
                app.Domain = HttpContext.Current.Request.Url.GetUrlHost();
                PlatformContext.Current.CreateAppContext(app);
            };
        }
Exemple #3
0
        /// <summary>
        /// 把数据表转换成List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List <T> Import <T>(this List <T> list, DataTable dt, out List <ImportMessage> errorMessages) where T : class, new()
        {
            var plist = new List <PropertyInfo>(typeof(T).GetProperties());

            errorMessages = new List <ImportMessage>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow item = dt.Rows[i];
                var     s    = Activator.CreateInstance <T>();
                foreach (DataColumn column in dt.Columns)
                {
                    var info = plist.FirstOrDefault(p => p.Name == column.ColumnName);
                    if (info == null)
                    {
                        continue;
                    }
                    if (item[column.ColumnName] == null)
                    {
                        continue;
                    }

                    dynamic dest;
                    var     isConvert = false;
                    try
                    {
                        isConvert = CoralConvert.Convert(item[column.ColumnName], info.PropertyType, out dest);
                    }
                    catch (Exception ex)
                    {
                        errorMessages.Add(new ImportMessage
                        {
                            Index        = i,
                            State        = ResultState.Fail,
                            ErrorMessage = string.Format("{0}的值:{1} 类型转换失败,{2}", column.ColumnName, item[column.ColumnName], ex.Message)
                        });
                        continue;
                    }

                    if (!isConvert)
                    {
                        errorMessages.Add(new ImportMessage
                        {
                            Index        = i,
                            State        = ResultState.Fail,
                            ErrorMessage = string.Format("{0}的值:{1} 类型转换失败", column.ColumnName, item[column.ColumnName])
                        });
                        continue;
                    }
                    info.SetValue(s, dest, null);
                }
                list.Add(s);
            }

            return(list);
        }
Exemple #4
0
        public static object GetSampleValue(string assembly, string typeFullName, string value)
        {
            var type = Assembly.Load(assembly).GetType(typeFullName);

            if (type == null)
            {
                return(null);
            }
            return(CoralConvert.Convert(value, type));
        }
Exemple #5
0
        /// <summary>
        /// 根据属性设置值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object SetValueByPropery <T>(this T entity, string propertyName, object value) where T : class
        {
            var property = typeof(T).GetProperties().FirstOrDefault(item => item.Name == propertyName);

            if (property == null)
            {
                return(null);
            }
            property.SetValue(entity, CoralConvert.Convert(value, property.PropertyType));
            return(property);
        }
Exemple #6
0
 public LocalCache()
 {
     if (AppConfig.IsExists(StaticString.DefaultRedisConfigDll) &&
         AppConfig.DllConfigs.Current.IsExists("Local") &&
         AppConfig.DllConfigs.Current["Local"].IsExists("Timeout"))
     {
         _timeout = CoralConvert.Convert <int>(AppConfig.DllConfigs.Current["Local"]["Timeout"]);
     }
     else
     {
         _timeout = 3600;
     }
 }
Exemple #7
0
        public static AppContext BuildAppContext(this PlatformContext platformContext)
        {
            var app = new AppModel
            {
                Id     = CoralConvert.Convert <long>(AppConfig.AppSettings["AppId"]),
                Key    = AppConfig.AppSettings["AppKey"],
                Secret = AppConfig.AppSettings["AppSecret"],
                Name   = AppConfig.AppSettings["AppName"],
                Code   = AppConfig.AppSettings["AppCode"],
                Domain = AppConfig.DllConfigs["Host"]["Servers"]["AppDomain"]
            };

            if (string.IsNullOrEmpty(app.Domain))
            {
                throw new ArgumentNullException("Domain", "请在Configs文件夹中Host配置文件的Servers节点配置AppDomain项的值");
            }
            return(platformContext.CreateAppContext(app));
        }
Exemple #8
0
        public static object GetCustomValue(string assemblyFullName, string typeFullName, List <XmlElement> elements)
        {
            var type = Assembly.Load(assemblyFullName).GetType(typeFullName);

            if (type == null)
            {
                return(null);
            }
            dynamic obj = Activator.CreateInstance(type, true);

            foreach (var element in elements)
            {
                var    property = type.GetProperty(element.Name);
                object value;
                if (!CoralConvert.Convert(GetValue(element), property.PropertyType, out value))
                {
                    continue;
                }
                property.SetValue(obj, value);
            }

            return(obj);
        }
Exemple #9
0
        /// <summary>
        /// 动态调用web服务
        /// </summary>
        /// <param name="url">WSDL服务地址</param>
        /// <param name="classname">类名</param>
        /// <param name="methodname">方法名</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static object InvokeWebService(string url, string classname, string methodname, Dictionary <string, object> args)
        {
            string @namespace = "";

            if ((classname == null) || (classname == ""))
            {
                classname = GetWsClassName(url);
            }
            try
            { //获取WSDL
                var wc     = new WebClient();
                var stream = wc.OpenRead(url + "?WSDL");
                var sd     = ServiceDescription.Read(stream);
                var sdi    = new ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                var cn = new CodeNamespace(@namespace);
                //生成客户端代理类代码
                var ccu = new CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                var icc = new CSharpCodeProvider();
                //设定编译参数
                var cplist = new CompilerParameters();
                cplist.GenerateExecutable = false;
                cplist.GenerateInMemory   = true;
                cplist.ReferencedAssemblies.Add("System.dll");
                cplist.ReferencedAssemblies.Add("System.XML.dll");
                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
                cplist.ReferencedAssemblies.Add("System.Data.dll");
                //编译代理类
                var cr = icc.CompileAssemblyFromDom(cplist, ccu);
                if (cr.Errors.HasErrors)
                {
                    var sb = new StringBuilder();
                    foreach (CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce);
                        sb.Append(Environment.NewLine);
                    }
                    throw new System.Exception(sb.ToString());
                }
                //生成代理实例,并调用方法
                var assembly   = cr.CompiledAssembly;
                var t          = assembly.GetType(@namespace + "." + classname, true, true);
                var service    = Activator.CreateInstance(t);
                var methodInfo = t.GetMethod(methodname);
                var paramsInfo = methodInfo.GetParameters();
                if (paramsInfo.Length != args.Count)
                {
                    throw new System.Exception("参数个数不匹配!");
                }
                var obj = new List <object>();
                for (int i = 0; i < paramsInfo.Length; i++)
                {
                    obj.Add(CoralConvert.Convert(args[paramsInfo[i].Name], paramsInfo[i].ParameterType));
                }
                return(methodInfo.Invoke(service, obj.ToArray()));
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message, new System.Exception(ex.StackTrace));
            }
        }