protected virtual AppPathAssembly CreateAppPathAssembly(string appPath, AssemblyManager manager)
        {
            AppPathAssembly result = new AppPathAssembly();

            if (!Directory.Exists(appPath))
            {
                return(result);
            }

            IEnumerable <string> binFiles = Directory.EnumerateFiles(appPath, "*.dll", SearchOption.TopDirectoryOnly);

            foreach (string fileName in binFiles)
            {
                try
                {
                    AssemblyName name = AssemblyName.GetAssemblyName(fileName);
                    if (!result.Constains(name))
                    {
                        Assembly assembly = manager.TryGetAssembly(name.FullName);
                        if (assembly == null)
                        {
                            assembly = NeitherContext ? manager.LoadAssembly(name, fileName) : manager.LoadAssembly(name);
                        }
                        result.Add(name, assembly);
                    }
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SLoadAssembly", GetType(), ex);
                }
            }

            return(result);
        }
        public void Finalize(object application)
        {
            var inits = AppPathAssembly.CreateInitializations();

            foreach (var item in inits)
            {
                try
                {
                    item.AppEnd(application);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("EAppEnd", item.GetType(), ex);
                }
            }
        }
        public void Initialize(BaseAppSetting appSetting, object application)
        {
            TkDebug.AssertArgumentNull(appSetting, "appSetting", this);

            fManager = new AssemblyManager();
            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            }

            // 初始化AppPath下所有的Assembly
            AppPathAssembly = CreateAppPathAssembly(appSetting.AppPath, fManager);
            TkDebug.AssertNotNull(AppPathAssembly, "CreateAppPathAssembly返回为空", this);

            //fLoadedAssembly = AppPathAssembly.LoadedAssembly;
            // AppPath下的Assembly中查找添加PlugInFactory
            AppPathAssembly.AddPlugInFactory(FactoryManager);
            // 执行AppPath下的IInitialization
            var inits = AppPathAssembly.CreateInitializations();

            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarting");
                    item.AppStarting(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarting", item.GetType(), ex);
                }
            }

            // 搜索Code插件
            foreach (var assembly in AppPathAssembly)
            {
                if (assembly == ToolkitConst.TOOLKIT_CORE_ASSEMBLY)
                {
                    continue;
                }
                LoadAssembly(assembly);
            }

            fPlugIn = new PlugInAssembly(appSetting, fManager);
            //IEnumerable<AssemblyName> moduleAssembyNames = GetModuleAssemblies(appSetting);
            foreach (var assemblyName in fPlugIn)
            {
                if (!fManager.Contains(assemblyName.Name))
                {
                    var assembly = fPlugIn.LoadAssembly(fManager, NeitherContext, assemblyName);
                    //Assembly assembly = CreateAssembly(assemblyName);
                    //fLoadedAssemblyDict.Add(assembly.FullName, assembly);
                    if (assembly != null)
                    {
                        LoadAssembly(assembly);
                        //fLoadedAssembly.Add(assemblyName.FullName);
                    }
                }
            }

            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            }

            // 搜索Xml插件
            var factories = FactoryManager.CodeFactories;

            foreach (var factory in factories)
            {
                factory.SearchXmlPlugIn(appSetting.XmlPath);
            }

            ProcessConfigDefaultValue(appSetting);
            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarted");
                    item.AppStarted(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarted", item.GetType(), ex);
                }
            }

            LogPlugError(fPlugInErrorLog);
        }
 private void InitializeToolkitCore()
 {
     AppPathAssembly.AddPlugInFactory(FactoryManager, ToolkitConst.TOOLKIT_CORE_ASSEMBLY);
     LoadAssembly(ToolkitConst.TOOLKIT_CORE_ASSEMBLY);
 }