/// <summary> /// 加载应用程序集 /// </summary> /// <param name="folder"></param> public override void LoadAssemblies() { if (PlusAssemblysList == null) { PlusAssemblysList = new List <IPlusInfo>(); } else { PlusAssemblysList.Clear(); } #region 插件文件目录 var plusFilesDirectoryInfo = new DirectoryInfo(SitePaths.PlusFilesDirPath); #endregion //插件前缀必须为Magicodes var plusDlls = plusFilesDirectoryInfo.GetFiles("*.dll", SearchOption.AllDirectories).ToList(); if (plusDlls.Count == 0) { return; } //插件程序集 //必须是Magicodes插件才会执行插件部署 //dll名称必须与插件目录名称一致才能部署 //{PlusDir}/{dllName} //{PlusDir}/bin/{dllName} var magicodesPlusDlls = plusDlls.Where(p => p.Name.StartsWith("Magicodes.") && (p.Directory.Name + ".dll" == p.Name || p.Directory.Parent.Name + ".dll" == p.Name)); //依赖的程序集 var orthersDlls = plusDlls.Where(p => !p.Name.StartsWith("Magicodes.")).Distinct(); #region 设置程序域 //var setup = new AppDomainSetup //{ // ApplicationName = "Magicodes.Core", // //DynamicBase = SitePaths.PlusShadowCopyDirPath, // PrivateBinPath = SitePaths.PlusShadowCopyDirPath, // DisallowCodeDownload = true, // ShadowCopyFiles = "true", // CachePath = SitePaths.PlusCacheDirPath, // ShadowCopyDirectories = SitePaths.PlusShadowCopyDirPath, // //PrivateBinPath = SitePaths.PlusShadowCopyDirPath //}; //var appDomain = AppDomain.CreateDomain("Magicodes.Core.Domain", null, setup); CurrentAppDomain = AppDomain.CurrentDomain; if (!CurrentAppDomain.IsFullyTrusted) { throw new MagicodesException("请将当前应用程序信任级别设置为完全信任。"); } #endregion var binDir = new DirectoryInfo(SitePaths.SiteRootBinDirPath); #region 部署依赖程序集 foreach (var plus in orthersDlls) { //如果网站bin目录不存在此dll,则将该dll复制到动态程序集目录 if (binDir.GetFiles(plus.Name).Length == 0 && PlusManager.DynamicDirectory.GetFiles(plus.Name).Length == 0) { PlusManager.CopyToDynamicDirectory(plus); Assembly assembly = Assembly.LoadFrom(plus.FullName); //将程序集添加到当前应用程序域 BuildManager.AddReferencedAssembly(assembly); } } #endregion #region 部署插件程序集 foreach (var plus in magicodesPlusDlls) { PlusManager.Deploy(plus); } #endregion //foreach (var plus in plusDlls) //{ // //必须是Magicodes插件才会执行插件部署 // //dll名称必须与插件目录名称一致才能部署 // //{PlusDir}/{dllName} // //{PlusDir}/bin/{dllName} // if (plus.Name.StartsWith("Magicodes.") && (plus.Directory.Name + ".dll" == plus.Name || plus.Directory.Parent.Name + ".dll" == plus.Name)) // PlusManager.Deploy(plus); // //如果网站bin目录不存在此dll,则将该dll复制到动态程序集目录 // else if (binDir.GetFiles(plus.Name).Length == 0 && PlusManager.DynamicDirectory.GetFiles(plus.Name).Length == 0) // { // PlusManager.CopyToDynamicDirectory(plus); // Assembly assembly = Assembly.LoadFrom(plus.FullName); // //将程序集添加到当前应用程序域 // BuildManager.AddReferencedAssembly(assembly); // } //} }
/// <summary> /// 加载应用程序集 /// </summary> /// <param name="folder"></param> public override void LoadAssemblies() { if (PlusAssemblysList == null) { PlusAssemblysList = new List <IPlusInfo>(); } else { PlusAssemblysList.Clear(); } #region 插件文件目录 var plusFilesDirectoryInfo = new DirectoryInfo(SitePaths.PlusFilesDirPath); #endregion //插件前缀必须为Magicodes var plusDlls = plusFilesDirectoryInfo.GetFiles("*.dll", SearchOption.AllDirectories).ToList(); if (plusDlls.Count == 0) { return; } //插件程序集 //必须是Magicodes插件才会执行插件部署 //dll名称必须与插件目录名称一致才能部署 //{PlusDir}/{dllName} //{PlusDir}/bin/{dllName} var magicodesPlusDlls = plusDlls.Where(p => p.Name.StartsWith("Magicodes.") && (p.Directory.Name + ".dll" == p.Name || p.Directory.Parent.Name + ".dll" == p.Name)); //依赖的程序集 var orthersDlls = plusDlls.Where(p => !p.Name.StartsWith("Magicodes.")).Distinct(); #region 设置程序域 //var setup = new AppDomainSetup //{ // ApplicationName = "Magicodes.Core", // //DynamicBase = SitePaths.PlusShadowCopyDirPath, // PrivateBinPath = SitePaths.PlusShadowCopyDirPath, // DisallowCodeDownload = true, // ShadowCopyFiles = "true", // CachePath = SitePaths.PlusCacheDirPath, // ShadowCopyDirectories = SitePaths.PlusShadowCopyDirPath, // //PrivateBinPath = SitePaths.PlusShadowCopyDirPath //}; //var appDomain = AppDomain.CreateDomain("Magicodes.Core.Domain", null, setup); CurrentAppDomain = AppDomain.CurrentDomain; if (!CurrentAppDomain.IsFullyTrusted) { throw new MagicodesException("请将当前应用程序信任级别设置为完全信任"); } #endregion var binDir = new DirectoryInfo(SitePaths.SiteRootBinDirPath); #region 部署依赖程序集 foreach (var plus in orthersDlls) { //如果网站bin目录不存在此dll,则将该dll复制到动态程序集目录 if (binDir.GetFiles(plus.Name).Length == 0 && PlusManager.DynamicDirectory.GetFiles(plus.Name).Length == 0) { PlusManager.CopyToDynamicDirectory(plus); Assembly assembly = Assembly.LoadFrom(plus.FullName); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); if (!assemblies.Any(p => p.FullName == assembly.FullName)) { //将程序集添加到当前应用程序域 BuildManager.AddReferencedAssembly(assembly); } } } #endregion #region 部署插件程序集 foreach (var plus in magicodesPlusDlls) { PlusManager.Deploy(plus); } #endregion #region 添加插件菜单 var r = APIContext <string> .Current.SiteAdminNavigationRepository; if (r == null) { return; } foreach (var plusInfo in PlusManager.PluginsList) { if (plusInfo.PlusConfigInfo != null && plusInfo.PlusConfigInfo.PlusMenus != null && plusInfo.PlusConfigInfo.PlusMenus.Length > 0) { if (r.GetQueryable().Any(p => p.PlusId == plusInfo.Id)) { r.RemoveRange(r.GetQueryable().Where(p => p.PlusId == plusInfo.Id)); r.SaveChanges(); } foreach (var plusMenu in plusInfo.PlusConfigInfo.PlusMenus) { AddPlusMenu(plusInfo, r, plusMenu, null); } r.SaveChanges(); } } #endregion }