public void RegisterFrom(CollectibleAssemblyLoadContext assemblyLoadContext)
        {
            var assembly = assemblyLoadContext.GetEntryPointAssembly();
            IEnumerable<Type> providers = assembly.GetExportedTypes().Where(p => p.GetInterfaces().Any(x => x.Name == "INotificationProvider"));

            if (providers.Any())
            {


                foreach (Type p in providers)
                {
                    INotificationProvider obj = (INotificationProvider)assembly.CreateInstance(p.FullName);
                    Dictionary<string, List<INotificationHandler>> result = obj.GetNotifications();

                    foreach (KeyValuePair<string, List<INotificationHandler>> item in result)
                    {
                        foreach (INotificationHandler i in item.Value)
                        {
                            
                            Subscribe(item.Key, i);
                            
                        }
                    }
                }
            }
        }
Example #2
0
        public void LoadStreamsIntoContext(CollectibleAssemblyLoadContext context, string moduleFolder, Assembly assembly)
        {
            AssemblyName[] references = assembly.GetReferencedAssemblies();

            foreach (AssemblyName item in references)
            {
                string name = item.Name;

                string version = item.Version.ToString();

                Stream stream = _referenceContainer.GetStream(name, version);

                if (stream != null)
                {
                    _logger.LogDebug($"Found the cached reference '{name}' v.{version}");
                    assembly = context.Assemblies.FirstOrDefault(a => a.GetName().Name == name);
                    if (assembly == null)
                    {
                        assembly = context.LoadFromStream(stream);
                    }
                }
                else
                {
                    if (IsSharedFreamwork(name) || IsLoadedByDefault(item.FullName))
                    {
                        continue;
                    }

                    string dllName  = $"{name}.dll";
                    string filePath = $"{moduleFolder}/{dllName}";

                    if (!File.Exists(filePath))
                    {
                        _logger.LogWarning($"The package '{dllName}' in '{filePath}' is missing.");
                        continue;
                    }

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

                        MemoryStream memoryStream = new MemoryStream();

                        fs.Position = 0;
                        fs.CopyTo(memoryStream);
                        fs.Position           = 0;
                        memoryStream.Position = 0;
                        _referenceContainer.SaveStream(name, version, memoryStream);
                    }
                }
                LoadStreamsIntoContext(context, moduleFolder, assembly);
            }
        }
        public void AddFromLoadContext(CollectibleAssemblyLoadContext assemblyLoadContext)
        {
            var assembly      = assemblyLoadContext.GetEntryPointAssembly();
            var pluginContext = assembly.GetExportedTypes().SingleOrDefault(p => p.GetInterfaces().Any(x => x.Name == nameof(IPluginContext)));

            if (pluginContext != null)
            {
                IPluginContext obj = (IPluginContext)assembly.CreateInstance(pluginContext.FullName);

                Add(assemblyLoadContext.PluginName, obj);
            }
        }
Example #4
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);
                }
            }
        }
Example #5
0
        public void EnableModule(string moduleName)
        {
            if (!_pluginsLoadContexts.Any(moduleName))
            {
                ReLoadModule(moduleName, false);
            }
            CollectibleAssemblyLoadContext context = _pluginsLoadContexts.Get(moduleName);

            foreach (var part in context.PluginAssemblyParts)
            {
                _partManager.ApplicationParts.Add(part);
            }

            context.Enable();
            //_pluginContextContainer.AddFromLoadContext(context);
            _notificationRegister.RegisterFrom(_pluginsLoadContexts.Get(moduleName));
            ResetControllActions();
            ModuleChangeEventHandler?.Invoke(ModuleEvent.Started, context);
        }
 public void Add(string pluginName, CollectibleAssemblyLoadContext context)
 {
     _pluginAssemblyLoadContext.Add(pluginName, context);
 }
 public void UnRegisterFrom(CollectibleAssemblyLoadContext assemblyLoadContext)
 {
     var assembly = assemblyLoadContext.GetEntryPointAssembly();
     foreach(var kp in _containers)
        kp.Value.RemoveAll(handler => handler.GetType().Assembly.GetName() == assembly.GetName());
 }