public CollectibleAssemblyLoadContext Get(string moduleName, IMvcBuilder mvcBuilder, IServiceScope scope, IDataStore dataStore)
        {
            CollectibleAssemblyLoadContext context = new CollectibleAssemblyLoadContext(moduleName);
            IReferenceLoader loader = scope.ServiceProvider.GetService <IReferenceLoader>();

            string filePath            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.dll");
            string viewFilePath        = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.Views.dll");
            string referenceFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName);

            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                Assembly assembly = context.LoadFromStream(fs);

                context.SetEntryPoint(assembly);

                loader.LoadStreamsIntoContext(context, referenceFolderPath, assembly);

                AssemblyPart controllerAssemblyPart = new AssemblyPart(assembly);
                mvcBuilder.PartManager.ApplicationParts.Add(controllerAssemblyPart);


                var resources = assembly.GetManifestResourceNames();

                if (resources.Any())
                {
                    foreach (var item in resources)
                    {
                        var stream = new MemoryStream();
                        var source = assembly.GetManifestResourceStream(item);
                        source.CopyTo(stream);

                        context.RegisterResource(item, stream.ToArray());
                    }
                }

                BuildNotificationProvider(assembly, scope);
                RegisterModuleQueries(dataStore, moduleName, assembly, scope);
            }

            using (FileStream fsView = new FileStream(viewFilePath, FileMode.Open))
            {
                Assembly viewAssembly = context.LoadFromStream(fsView);
                loader.LoadStreamsIntoContext(context, referenceFolderPath, viewAssembly);

                MystiqueRazorAssemblyPart moduleView = new MystiqueRazorAssemblyPart(viewAssembly, moduleName);
                mvcBuilder.PartManager.ApplicationParts.Add(moduleView);
            }

            context.Enable();

            return(context);
        }
        public CollectibleAssemblyLoadContext Get(string moduleName, ApplicationPartManager apm, IServiceScope scope, IDataStore dataStore)
        {
            CollectibleAssemblyLoadContext context = new CollectibleAssemblyLoadContext(moduleName);
            IReferenceLoader loader = scope.ServiceProvider.GetService <IReferenceLoader>();

            string filePath            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.dll");
            string viewFilePath        = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.Views.dll");
            string referenceFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName);

            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                Assembly assembly = context.LoadFromStream(fs);

                context.SetEntryPoint(assembly);

                loader.LoadStreamsIntoContext(context, referenceFolderPath, assembly);

                AssemblyPart controllerAssemblyPart = new AssemblyPart(assembly);
                apm.ApplicationParts.Add(controllerAssemblyPart);

                BuildNotificationProvider(assembly, scope);
                RegisterModuleQueries(dataStore, moduleName, assembly, scope);
            }

            using (FileStream fsView = new FileStream(viewFilePath, FileMode.Open))
            {
                Assembly viewAssembly = context.LoadFromStream(fsView);
                loader.LoadStreamsIntoContext(context, referenceFolderPath, viewAssembly);

                MystiqueRazorAssemblyPart moduleView = new MystiqueRazorAssemblyPart(viewAssembly, moduleName);
                apm.ApplicationParts.Add(moduleView);
            }

            context.Enable();

            return(context);
        }
Esempio n. 3
0
        public static void MystiqueSetup(this IServiceCollection services, IConfiguration configuration)
        {
            _serviceCollection = services;

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IMvcModuleSetup, MvcModuleSetup>();
            services.AddScoped <IPluginManager, PluginManager>();
            services.AddScoped <ISystemManager, SystemManager>();
            services.AddScoped <IUnitOfWork, Repository.MySql.UnitOfWork>();
            services.AddSingleton <INotificationRegister, NotificationRegister>();
            services.AddSingleton <IActionDescriptorChangeProvider>(MystiqueActionDescriptorChangeProvider.Instance);
            services.AddSingleton <IReferenceContainer, DefaultReferenceContainer>();
            services.AddSingleton <IReferenceLoader, DefaultReferenceLoader>();
            services.AddSingleton(MystiqueActionDescriptorChangeProvider.Instance);

            IMvcBuilder mvcBuilder = services.AddMvc();

            ServiceProvider provider = services.BuildServiceProvider();

            using (IServiceScope scope = provider.CreateScope())
            {
                IUnitOfWork unitOfWork = scope.ServiceProvider.GetService <IUnitOfWork>();

                if (unitOfWork.CheckDatabase())
                {
                    List <ViewModels.PluginListItemViewModel> allEnabledPlugins = unitOfWork.PluginRepository.GetAllEnabledPlugins();
                    IReferenceLoader loader = scope.ServiceProvider.GetService <IReferenceLoader>();

                    foreach (ViewModels.PluginListItemViewModel plugin in allEnabledPlugins)
                    {
                        CollectibleAssemblyLoadContext context = new CollectibleAssemblyLoadContext(plugin.Name);
                        string moduleName = plugin.Name;

                        string filePath            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.dll");
                        string viewFilePath        = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName, $"{moduleName}.Views.dll");
                        string referenceFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules", moduleName);

                        _presets.Add(filePath);
                        using (FileStream fs = new FileStream(filePath, FileMode.Open))
                        {
                            Assembly assembly = context.LoadFromStream(fs);
                            context.SetEntryPoint(assembly);

                            loader.LoadStreamsIntoContext(context, referenceFolderPath, assembly);

                            MystiqueAssemblyPart controllerAssemblyPart = new MystiqueAssemblyPart(assembly);
                            mvcBuilder.PartManager.ApplicationParts.Add(controllerAssemblyPart);
                            PluginsLoadContexts.Add(plugin.Name, context);

                            BuildNotificationProvider(assembly, scope);
                        }

                        using (FileStream fsView = new FileStream(viewFilePath, FileMode.Open))
                        {
                            Assembly viewAssembly = context.LoadFromStream(fsView);
                            loader.LoadStreamsIntoContext(context, referenceFolderPath, viewAssembly);

                            MystiqueRazorAssemblyPart moduleView = new MystiqueRazorAssemblyPart(viewAssembly, moduleName);
                            mvcBuilder.PartManager.ApplicationParts.Add(moduleView);
                        }

                        context.Enable();
                    }
                }
            }

            AssemblyLoadContextResoving();

            services.Configure <RazorViewEngineOptions>(o =>
            {
                o.AreaViewLocationFormats.Add("/Modules/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
                o.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
            });

            services.Replace <IViewCompilerProvider, MystiqueViewCompilerProvider>();
        }