public void EnableModule(string moduleName) { if (!PluginsLoadContexts.Any(moduleName)) { var context = new CollectibleAssemblyLoadContext(); var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll"; var referenceFolderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}"; using (var fs = new FileStream(filePath, FileMode.Open)) { var assembly = context.LoadFromStream(fs); DefaultReferenceLoader loader = new DefaultReferenceLoader(referenceFolderPath, $"{moduleName}.dll"); loader.LoadStreamsIntoContext(context); var controllerAssemblyPart = new MystiqueAssemblyPart(assembly); AdditionalReferencePathHolder.AdditionalReferencePaths.Add(filePath); _partManager.ApplicationParts.Add(controllerAssemblyPart); PluginsLoadContexts.AddPluginContext(moduleName, context); } } else { var context = PluginsLoadContexts.GetContext(moduleName); var controllerAssemblyPart = new MystiqueAssemblyPart(context.Assemblies.First()); _partManager.ApplicationParts.Add(controllerAssemblyPart); } ResetControllActions(); }
public static void MystiqueSetup(this IServiceCollection services, IConfiguration configuration) { services.AddOptions(); services.Configure <ConnectionStringSetting>(configuration.GetSection("ConnectionStringSetting")); services.AddSingleton <IMvcModuleSetup, MvcModuleSetup>(); services.AddScoped <IPluginManager, PluginManager>(); services.AddScoped <IUnitOfWork, UnitOfWork>(); services.AddSingleton <IActionDescriptorChangeProvider>(MystiqueActionDescriptorChangeProvider.Instance); services.AddSingleton(MystiqueActionDescriptorChangeProvider.Instance); var mvcBuilder = services.AddMvc().AddRazorRuntimeCompilation(o => { foreach (var item in _presets) { o.AdditionalReferencePaths.Add(item); } AdditionalReferencePathHolder.AdditionalReferencePaths = o.AdditionalReferencePaths; }); var provider = services.BuildServiceProvider(); using (var scope = provider.CreateScope()) { var option = scope.ServiceProvider.GetService <MvcRazorRuntimeCompilationOptions>(); var unitOfWork = scope.ServiceProvider.GetService <IUnitOfWork>(); var allEnabledPlugins = unitOfWork.PluginRepository.GetAllEnabledPlugins(); foreach (var plugin in allEnabledPlugins) { var context = new CollectibleAssemblyLoadContext(); var moduleName = plugin.Name; var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll"; var referenceFolderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}"; _presets.Add(filePath); using (var fs = new FileStream(filePath, FileMode.Open)) { var assembly = context.LoadFromStream(fs); DefaultReferenceLoader loader = new DefaultReferenceLoader(referenceFolderPath, $"{moduleName}.dll"); loader.LoadStreamsIntoContext(context); var controllerAssemblyPart = new MystiqueAssemblyPart(assembly); mvcBuilder.PartManager.ApplicationParts.Add(controllerAssemblyPart); PluginsLoadContexts.AddPluginContext(plugin.Name, context); } } } services.Configure <RazorViewEngineOptions>(o => { o.AreaViewLocationFormats.Add("/Modules/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension); o.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml"); }); }
public IActionResult Enable(Guid id) { var module = _pluginManager.GetPlugin(id); if (!PluginsLoadContexts.Any(module.Name)) { var context = new CollectibleAssemblyLoadContext(); _pluginManager.EnablePlugin(id); var moduleName = module.Name; var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll"; using (var fs = new FileStream(filePath, FileMode.Open)) { var assembly = context.LoadFromStream(fs); var controllerAssemblyPart = new MyAssemblyPart(assembly); AdditionalReferencePathHolder.AdditionalReferencePaths.Add(filePath); _partManager.ApplicationParts.Add(controllerAssemblyPart); } MyActionDescriptorChangeProvider.Instance.HasChanged = true; MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel(); PluginsLoadContexts.AddPluginContext(module.Name, context); } else { var context = PluginsLoadContexts.GetContext(module.Name); var controllerAssemblyPart = new AssemblyPart(context.Assemblies.First()); _partManager.ApplicationParts.Add(controllerAssemblyPart); _pluginManager.EnablePlugin(id); MyActionDescriptorChangeProvider.Instance.HasChanged = true; MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel(); } return(RedirectToAction("Index")); }