コード例 #1
0
        public static IApplicationBuilder UsePlugin(this IApplicationBuilder applicationBuilder)
        {
            var           serviceProvider = applicationBuilder.ApplicationServices;
            PluginOptions _pluginOptions  = serviceProvider.GetService <IOptions <PluginOptions> >().Value;

            if (!_pluginOptions.Enable)
            {
                return(applicationBuilder);
            }
            MvcRazorRuntimeCompilationOptions option = serviceProvider.GetService <IOptions <MvcRazorRuntimeCompilationOptions> >()?.Value;

            var pluginsLoadContexts = serviceProvider.GetService <IPluginsAssemblyLoadContexts>();
            //AssemblyLoadContextResoving(pluginsLoadContexts);
            IReferenceLoader loader                 = serviceProvider.GetService <IReferenceLoader>();
            var moduleSetup                         = serviceProvider.GetService <IMvcModuleSetup>();
            IPluginManagerService pluginManager     = serviceProvider.GetService <IPluginManagerService>();
            List <PluginInfoDto>  allEnabledPlugins = pluginManager.GetAllPlugins().ConfigureAwait(false).GetAwaiter().GetResult();

            ModuleChangeDelegate moduleStarted = (moduleEvent, context) =>
            {
                if (context?.PluginContext == null || context?.PluginContext.PluginEntityAssemblies.Count() == 0)
                {
                    return;
                }

                //var pluginContexts = pluginsLoadContexts.All().Select(c => c.PluginContext).SkipWhile(c => c == null);
                switch (moduleEvent)
                {
                case ModuleEvent.Installed:
                    HandleModuleInstallEvent(serviceProvider, context);
                    break;

                case ModuleEvent.Loaded:
                    HandleModuleLoadEvent(serviceProvider, context);
                    break;

                case ModuleEvent.Started:
                    break;

                case ModuleEvent.Stoped:
                    break;

                case ModuleEvent.UnInstalled:
                    HandleModuleUnintallEvent(serviceProvider, context);
                    break;

                default:
                    break;
                }
            };

            moduleSetup.ModuleChangeEventHandler += moduleStarted;
            var env = serviceProvider.GetService <IHostEnvironment>();

            foreach (var plugin in allEnabledPlugins)
            {
                string filePath = Path.Combine(env.ContentRootPath, _pluginOptions.InstallBasePath, plugin.Name, $"{ plugin.Name}.dll");
                //option.FileProviders.Add(new PhysicalFileProvider(Path.Combine(AppContext.BaseDirectory, _pluginOptions.InstallBasePath, plugin.Name)));
                option.AdditionalReferencePaths.Add(filePath);
                if (plugin.IsEnable)
                {
                    moduleSetup.EnableModule(plugin.Name);
                }
                else
                {
                    moduleSetup.LoadModule(plugin.Name, false);
                }
            }

            AdditionalReferencePathHolder.AdditionalReferencePaths = option?.AdditionalReferencePaths;
            var razorViewEngineOptions = serviceProvider.GetService <IOptions <RazorViewEngineOptions> >()?.Value;

            razorViewEngineOptions?.AreaViewLocationFormats.Add("/Plugins/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
            razorViewEngineOptions?.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");


            return(applicationBuilder);
        }
コード例 #2
0
        public async Task <ResponseResult <List <PluginInfoDto> > > GetPlugins()
        {
            var plugins = await _pluginManager.GetAllPlugins();

            return(new ResponseResult <List <PluginInfoDto> >(plugins));
        }