/// <summary> /// 加载插件程序集到应用程序域中 /// </summary> public static void Load() { try { //插件目录 DirectoryInfo pluginFolder = new DirectoryInfo(IOHelper.GetMapPath(_pluginfolderpath)); if (!pluginFolder.Exists) { pluginFolder.Create(); } //插件bin目录 DirectoryInfo shadowFolder = new DirectoryInfo(IOHelper.GetMapPath(_shadowfolderpath)); if (!shadowFolder.Exists) { shadowFolder.Create(); } else { //清空影子复制目录中的dll文件 foreach (FileInfo fileInfo in shadowFolder.GetFiles()) { fileInfo.Delete(); } } //获得安装的插件系统名称列表 List <string> installedPluginSystemNameList = GetInstalledPluginSystemNameList(); //获得全部插件 List <KeyValuePair <FileInfo, PluginInfo> > allPluginFileAndInfo = GetAllPluginFileAndInfo(pluginFolder); foreach (KeyValuePair <FileInfo, PluginInfo> fileAndInfo in allPluginFileAndInfo) { FileInfo pluginFile = fileAndInfo.Key; PluginInfo pluginInfo = fileAndInfo.Value; if (String.IsNullOrWhiteSpace(pluginInfo.SystemName)) { throw new BMAException(string.Format("插件'{0}'没有\"systemName\", 请输入一个唯一的\"systemName\"", pluginFile.FullName)); } if (pluginInfo.Type != 0 && pluginInfo.Type != 1) { throw new BMAException(string.Format("插件'{0}'不属于任何一种类型, 请输入正确的的\"type\"", pluginFile.FullName)); } //加载插件dll文件 FileInfo[] dllFiles = pluginFile.Directory.GetFiles("*.dll", SearchOption.TopDirectoryOnly); foreach (FileInfo dllFile in dllFiles) { //部署dll文件 DeployDllFile(dllFile, shadowFolder); } if (IsInstalledlPlugin(pluginInfo.SystemName, installedPluginSystemNameList))//安装的插件 { //根据插件类型将插件添加到相应列表 switch (pluginInfo.Type) { case 0: _oauthpluginlist.Add(pluginInfo); break; case 1: _paypluginlist.Add(pluginInfo); break; } } else//未安装的插件 { _uninstalledpluginlist.Add(pluginInfo); } } } catch (Exception ex) { throw new BMAException("加载NStore插件时出错", ex); } }
/// <summary> /// 设置默认插件 /// </summary> /// <param name="systemName">插件系统名称</param> public static void Default(string systemName) { lock (_locker) { if (string.IsNullOrEmpty(systemName)) { return; } PluginInfo pluginInfo = null; Predicate <PluginInfo> condition = x => x.SystemName.Equals(systemName, StringComparison.InvariantCultureIgnoreCase); pluginInfo = _oauthpluginlist.Find(condition); if (pluginInfo == null) { pluginInfo = _paypluginlist.Find(condition); } if (pluginInfo == null) { pluginInfo = _uninstalledpluginlist.Find(condition); } //当插件为空时直接返回 if (pluginInfo == null) { return; } List <PluginInfo> updatePluginList = new List <PluginInfo>(); if (pluginInfo.Type == 0) { foreach (PluginInfo info in _oauthpluginlist) { if (info.IsDefault == 1) { info.IsDefault = 0; updatePluginList.Add(info); } } } else { foreach (PluginInfo info in _paypluginlist) { if (info.IsDefault == 1) { info.IsDefault = 0; updatePluginList.Add(info); } } } foreach (PluginInfo info in _uninstalledpluginlist) { if (info.Type == pluginInfo.Type && info.IsDefault == 1) { info.IsDefault = 0; updatePluginList.Add(info); } } pluginInfo.IsDefault = 1; updatePluginList.Add(pluginInfo); //将插件信息持久化到对应文件中 foreach (PluginInfo info in updatePluginList) { IOHelper.SerializeToXml(info, IOHelper.GetMapPath("/plugins/" + info.Folder + "/plugin.config")); } } }