/// <summary>
        /// 加载程序集信息
        /// </summary>
        private void LoadAssemblyDescriptor(ModuleDescriptor moduleDescriptor)
        {
            //此处默认模块命名空间前缀与当前项目相同
            var assemblyDescriptor = new ModuleAssemblyDescriptor
            {
                Domain         = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Domain"),
                Infrastructure = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Infrastructure"),
                Application    = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Application"),
                Quartz         = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Quartz"),
                Web            = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Web"),
                Api            = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Api"),
            };

            CheckAssemblyDescriptor(moduleDescriptor, assemblyDescriptor);

            moduleDescriptor.AssemblyDescriptor = assemblyDescriptor;

            //加载模块初始化器
            var controllerAssembly = assemblyDescriptor.Web ?? assemblyDescriptor.Api;

            if (controllerAssembly != null)
            {
                var initializerType = controllerAssembly.GetTypes().FirstOrDefault(t => typeof(IModuleInitializer).IsAssignableFrom(t));
                if (initializerType != null && (initializerType != typeof(IModuleInitializer)))
                {
                    moduleDescriptor.Initializer = (IModuleInitializer)Activator.CreateInstance(initializerType);
                }
            }
        }
Exemple #2
0
        public ModuleCollection()
        {
            var allAssembly = AssemblyHelper.Load();

            foreach (var assembly in allAssembly)
            {
                var resources = assembly.GetManifestResourceNames();
                var name      = resources.FirstOrDefault(m => m.EndsWith("_module.json"));
                if (name.IsNull())
                {
                    continue;
                }

                var stream = assembly.GetManifestResourceStream(name);
                if (stream == null)
                {
                    continue;
                }

                //读取流并解析配置信息
                var moduleDescriptor = JsonConvert.DeserializeObject <ModuleDescriptor>(new StreamReader(stream).ReadToEnd());
                if (moduleDescriptor != null)
                {
                    //判断是否已存在
                    if (!_list.Any(m => m.Name.Equals(moduleDescriptor.Name)))
                    {
                        //此处默认模块命名空间前缀与当前项目相同
                        var assemblyDescriptor = new ModuleAssemblyDescriptor
                        {
                            Domain         = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Domain"),
                            Infrastructure = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Infrastructure"),
                            Application    = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Application"),
                            Quartz         = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Quartz"),
                            Web            = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Web"),
                            Api            = AssemblyHelper.LoadByNameEndString($"Module.{moduleDescriptor.Id}.Api"),
                        };

                        Check.NotNull(assemblyDescriptor.Domain, moduleDescriptor.Id + "模块的Domain程序集未发现");
                        Check.NotNull(assemblyDescriptor.Infrastructure, moduleDescriptor.Id + "模块的Infrastructure程序集未发现");
                        Check.NotNull(assemblyDescriptor.Application, moduleDescriptor.Id + "模块的Application程序集未发现");

                        var controllerAssembly = assemblyDescriptor.Web ?? assemblyDescriptor.Api;
                        if (controllerAssembly != null)
                        {
                            var initializerType = controllerAssembly.GetTypes().FirstOrDefault(t => typeof(IModuleInitializer).IsAssignableFrom(t));
                            if (initializerType != null && (initializerType != typeof(IModuleInitializer)))
                            {
                                moduleDescriptor.Initializer = (IModuleInitializer)Activator.CreateInstance(initializerType);
                            }
                        }

                        moduleDescriptor.AssemblyDescriptor = assemblyDescriptor;

                        Add(moduleDescriptor);
                    }
                }
            }
        }