Пример #1
0
        /// <summary>
        /// 注册指定的类型到IOC容器中
        /// </summary>
        /// <param name="objectConfiguration"></param>
        private void RegisterTypeOrInstanceToUnitySinglon(Config.ObjectItem objectConfiguration)
        {
            try
            {
                //判断配置的值
                if (string.IsNullOrEmpty(objectConfiguration.Name) || string.IsNullOrEmpty(objectConfiguration.Interface) || string.IsNullOrEmpty(objectConfiguration.Implement))
                {
                    return;
                }

                RegisterSinglonTypeToUnity(objectConfiguration.Implement, objectConfiguration.LifeCycle);
            }
            catch (ReflectionTypeLoadException ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Exception exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    if (exSub is FileNotFoundException)
                    {
                        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
                        if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                        {
                            sb.AppendLine("Fusion Log:");
                            sb.AppendLine(exFileNotFound.FusionLog);
                        }
                    }
                    sb.AppendLine();
                }
                string errorMessage = sb.ToString();

                log4netLogger.Debug("注册类型的过程中出现错误" + errorMessage);

#if DEBUG
                throw;
#endif
            }
        }
Пример #2
0
        private void RegisterTypeOrInstanceToUnity(Config.ObjectItem objectConfiguration)
        {
            try
            {
                //判断配置的值
                if (string.IsNullOrEmpty(objectConfiguration.Name) || string.IsNullOrEmpty(objectConfiguration.Interface) || string.IsNullOrEmpty(objectConfiguration.Implement))
                {
                    return;
                }

                //获取程序集名称,判断是否已经有后缀
                string assemblyName = objectConfiguration.Implement;

                string[] assemblyInfos = assemblyName.Split(';');
                if (assemblyInfos.Length != 2)
                {
                    return;
                }

                assemblyName = assemblyInfos[0];

                if (assemblyName.LastIndexOf(".dll") < 0)
                {
                    assemblyName += ".dll";
                }

                string assemblyPath = System.IO.Path.Combine(PlatformConfig.ServerConfig.IOCSetting.AssemblyFilePath, assemblyName);

                //判断程序集是否存在
                if (!System.IO.File.Exists(assemblyPath))
                {
                    return;
                }

                //装载程序集
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(assemblyPath);

                //如果装载失败
                if (assembly == null)
                {
                    return;
                }

                //反射得到所有的程序集中的类型
                Type[] types = assembly.GetTypes();

                //循环所有的类型,准备注册
                foreach (var type in types)
                {
                    if (type.IsAbstract || type.IsInterface || type.IsGenericType)
                    {
                        continue;
                    }

                    //if (Config.PlatformConfig.ServerConfig.IOCSetting.InterceptionItems.Where(pre => System.IO.Path.GetFileNameWithoutExtension(pre.AssemblyName) == (System.IO.Path.GetFileNameWithoutExtension(objectConfiguration.Value))).Count() == 0) //判断如果注册拦截其器则拦截,否则不拦截
                    //    RegisterType(type, objectConfiguration.LifeCycle);
                    //else
                    RegisterInterceptionType(type, "Singleton");
                }
            }
            catch (ReflectionTypeLoadException ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Exception exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    if (exSub is FileNotFoundException)
                    {
                        FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
                        if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                        {
                            sb.AppendLine("Fusion Log:");
                            sb.AppendLine(exFileNotFound.FusionLog);
                        }
                    }
                    sb.AppendLine();
                }
                string errorMessage = sb.ToString();

                log4netLogger.Debug("注册类型的过程中出现错误" + errorMessage);
#if DEBUG
                throw;
#endif
            }
        }