예제 #1
0
        public void LoadModule(string moduleName, bool isInstall = true)
        {
            if (!_pluginsLoadContexts.Any(moduleName))
            {
                CollectibleAssemblyLoadContext context = new CollectibleAssemblyLoadContext(moduleName);

                string filePath            = Path.Combine(_baseDirectory, _pluginOptions.InstallBasePath, moduleName, $"{moduleName}.dll");
                string referenceFolderPath = Path.Combine(_baseDirectory, _pluginOptions.InstallBasePath, moduleName);
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    Assembly assembly = context.LoadFromStream(fs);
                    _referenceLoader.LoadStreamsIntoContext(context, referenceFolderPath, assembly);

                    context.SetEntryPoint(assembly);
                    context.PluginAssemblyParts.Add(new PluginAssemblyPart(assembly));
                    if (!AdditionalReferencePathHolder.AdditionalReferencePaths.Contains(filePath))
                    {
                        AdditionalReferencePathHolder.AdditionalReferencePaths.Add(filePath);
                    }
                }
                var viewsFilePath = Path.Combine(_baseDirectory, _pluginOptions.InstallBasePath, moduleName, $"{moduleName}.Views.dll");
                if (File.Exists(viewsFilePath))
                {
                    using (FileStream fs = new FileStream(viewsFilePath, FileMode.Open))
                    {
                        Assembly assembly = context.LoadFromStream(fs);
                        context.PluginAssemblyParts.Add(new CompiledRazorAssemblyPart(assembly));
                        _referenceLoader.LoadStreamsIntoContext(context, referenceFolderPath, assembly);
                    }
                }
                var pluginWebRoot = Path.Combine(_baseDirectory, _pluginOptions.InstallBasePath, moduleName, $"wwwroot");
                DirectoryCopy(pluginWebRoot, _env.WebRootPath, true);
                _pluginsLoadContexts.Add(moduleName, context);
                if (isInstall)
                {
                    ModuleChangeEventHandler?.Invoke(ModuleEvent.Installed, context);
                }
                else
                {
                    ModuleChangeEventHandler?.Invoke(ModuleEvent.Loaded, context);
                }
            }
        }