public static void AddPluginFramework(this IServiceCollection services) { // 用于添加插件Controller 时,通知Controller.Action发生变化 services.AddSingleton <IActionDescriptorChangeProvider>(PluginActionDescriptorChangeProvider.Instance); services.AddSingleton(PluginActionDescriptorChangeProvider.Instance); services.AddTransient <PluginControllerManager>(); services.AddTransient <PluginManager>(); services.AddTransient <PluginFinder>(); using (var serviceProvider = services.BuildServiceProvider()) { using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { PluginManager pluginManager = scope.ServiceProvider.GetService <PluginManager>(); // 在程序启动时加载所有 已安装的插件 // 获取PluginConfigModel #region 获取 获取PluginConfigModel PluginConfigModel pluginConfigModel = PluginConfigModelFactory.Create(); #endregion // 已启用的插件 #region 加载 已启用插件的Assemblies IList <string> enabledPluginIds = pluginConfigModel.EnabledPlugins; foreach (var pluginId in enabledPluginIds) { pluginManager.LoadPlugin(pluginId); } #endregion } } }
/// <summary> /// 插件 /// </summary> /// <param name="query"></param> /// <returns></returns> public async Task <ActionResult <ResponseModel> > Plugins(string query = "") { ResponseModel responseDTO = new ResponseModel(); IList <PluginRegistryResponseModel> pluginRegistryModels = new List <PluginRegistryResponseModel>(); try { // 1. TODO: 从json文件中读取插件订阅源 registry url string registryUrl = ""; // 2. TODO: 向订阅源发送 http get 获取插件列表信息 eg: http://rem-core-plugins-registry.moeci.com/?query=xxx IList <string> remotePluginIds = new List <string>(); // 3. 根据本地已有 PluginId 插件情况 状态赋值 PluginConfigModel pluginConfigModel = PluginConfigModelFactory.Create(); IList <string> localPluginIds = pluginConfigModel.EnabledPlugins.Concat(pluginConfigModel.DisabledPlugins).Concat(pluginConfigModel.UninstalledPlugins).ToList(); responseDTO.code = 1; responseDTO.message = "获取远程插件数据成功"; responseDTO.data = pluginRegistryModels; } catch (Exception ex) { responseDTO.code = -1; responseDTO.message = "获取远程插件数据失败: " + ex.Message; responseDTO.data = pluginRegistryModels; } return(await Task.FromResult(responseDTO)); }
public async Task <ActionResult <ResponseModel> > Install(string pluginId) { ResponseModel responseData = new ResponseModel(); PluginConfigModel pluginConfigModel = PluginConfigModelFactory.Create(); // TODO: 效验 #region 效验 if (string.IsNullOrEmpty(pluginId)) { responseData.code = -1; responseData.message = "安装失败, pluginId不能为空"; return(await Task.FromResult(responseData)); } #endregion try { // 1. 从 pluginConfigModel.UninstalledPlugins 移除, 添加到 pluginConfigModel.DisabledPlugins pluginConfigModel.UninstalledPlugins.Remove(pluginId); pluginConfigModel.DisabledPlugins.Add(pluginId); // 2.保存到 plugin.config.json PluginConfigModelFactory.Save(pluginConfigModel); responseData.code = 1; responseData.message = "安装成功"; } catch (Exception ex) { responseData.code = -1; responseData.message = "安装失败: " + ex.Message; return(await Task.FromResult(responseData)); } return(await Task.FromResult(responseData)); }
public static PluginConfigModel Create() { PluginConfigModel pluginConfigModel = new PluginConfigModel(); string pluginConfigFilePath = Path.Combine(Directory.GetCurrentDirectory(), "App_Data", "plugin.config.json"); string pluginConfigJsonStr = File.ReadAllText(pluginConfigFilePath, Encoding.UTF8); JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(); jsonSerializerOptions.PropertyNameCaseInsensitive = true; pluginConfigModel = JsonSerializer.Deserialize <PluginConfigModel>(pluginConfigJsonStr, jsonSerializerOptions); return(pluginConfigModel); }
public static void Save(PluginConfigModel pluginConfigModel) { if (pluginConfigModel == null) { throw new ArgumentNullException(nameof(pluginConfigModel)); } try { string pluginConfigJsonStr = JsonSerializer.Serialize(pluginConfigModel); string pluginConfigFilePath = Path.Combine(Directory.GetCurrentDirectory(), "App_Data", "plugin.config.json"); File.WriteAllText(pluginConfigFilePath, pluginConfigJsonStr, Encoding.UTF8); } catch (Exception ex) { } }
private IList <PluginInfoResponseModel> PluginInfoModelToResponseModel(IList <PluginInfoModel> pluginInfoModels, PluginConfigModel pluginConfigModel) { IList <PluginInfoResponseModel> responseModels = new List <PluginInfoResponseModel>(); #region 添加插件状态信息 foreach (var model in pluginInfoModels) { PluginInfoResponseModel responseModel = new PluginInfoResponseModel(); responseModel.Author = model.Author; responseModel.Description = model.Description; responseModel.DisplayName = model.DisplayName; responseModel.PluginId = model.PluginId; responseModel.SupportedVersions = model.SupportedVersions; responseModel.Version = model.Version; if (pluginConfigModel.EnabledPlugins.Contains(model.PluginId)) { responseModel.Status = PluginStatus.Enabled; } else if (pluginConfigModel.DisabledPlugins.Contains(model.PluginId)) { responseModel.Status = PluginStatus.Disabled; } else if (pluginConfigModel.UninstalledPlugins.Contains(model.PluginId)) { responseModel.Status = PluginStatus.Uninstalled; } responseModels.Add(responseModel); } #endregion return(responseModels); }