public void ConfigureServices(IServiceCollection services)
        {
            var mvcBuilder = services
                             .AddMvc()
                             .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            foreach (var dir in Directory.GetDirectories(Path.Combine(AppContext.BaseDirectory, "plugins")))
            {
                var pluginName     = Path.GetFileName(dir);
                var plugin         = PluginLoader.CreateFromAssemblyFile(Path.Combine(dir, pluginName + ".dll"));
                var pluginAssembly = plugin.LoadDefaultAssembly();
                Console.WriteLine($"Loading application parts from plugin {pluginName}");

                // This loads MVC application parts from plugin assemblies
                var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);
                foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
                {
                    Console.WriteLine($"* {part.Name}");
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }

                // This piece finds and loads related parts, such as MvcAppPlugin1.Views.dll.
                var relatedAssembliesAttrs = pluginAssembly.GetCustomAttributes <RelatedAssemblyAttribute>();
                foreach (var attr in relatedAssembliesAttrs)
                {
                    var assembly = plugin.LoadAssembly(attr.AssemblyFileName);
                    partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                    foreach (var part in partFactory.GetApplicationParts(assembly))
                    {
                        Console.WriteLine($"  * {part.Name}");
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }
            }
        }
Example #2
0
        private static void AddApplicationPart(IMvcCoreBuilder mvcCoreBuilder,
                                               Assembly assembly, string systemName, string filename)
        {
            try
            {
                //we can now register the plugin definition
                Log.Information("Adding to ApplicationParts: '{0}'", systemName);
                mvcCoreBuilder.AddApplicationPart(assembly);

                var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: false);
                foreach (var relatedAssembly in relatedAssemblies)
                {
                    var applicationPartFactory = ApplicationPartFactory.GetApplicationPartFactory(relatedAssembly);
                    foreach (var part in applicationPartFactory.GetApplicationParts(relatedAssembly))
                    {
                        mvcCoreBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "PluginManager");
                throw new InvalidOperationException($"The plugin directory for the {systemName} file exists in a folder outside of the allowed grandnode folder hierarchy - exception because of {filename} - exception: {ex.Message}");
            }
        }
Example #3
0
        /// <summary>添加自定义应用部分,即添加外部引用的控制器、视图的Assembly,作为本应用的一部分</summary>
        /// <param name="services"></param>
        public static void AddCustomApplicationParts(this IServiceCollection services)
        {
            var manager = services.LastOrDefault(e => e.ServiceType == typeof(ApplicationPartManager))?.ImplementationInstance as ApplicationPartManager;

            if (manager == null)
            {
                manager = new ApplicationPartManager();
            }

            var list = FindAllArea();

            foreach (var asm in list)
            {
                XTrace.WriteLine("注册区域视图程序集:{0}", asm.FullName);

                var factory = ApplicationPartFactory.GetApplicationPartFactory(asm);
                foreach (var part in factory.GetApplicationParts(asm))
                {
                    if (!manager.ApplicationParts.Contains(part))
                    {
                        manager.ApplicationParts.Add(part);
                    }
                }
            }
        }
Example #4
0
        public IActionResult LoadModule()
        {
            var siteModulePath = Path.Combine(AppContext.BaseDirectory, "Modules");

            var siteModules = PluginLoader.LoadModules(siteModulePath, new[]
            {
                typeof(IApplicationBuilder),
                typeof(IPlugin),
                typeof(IServiceCollection),
            });

            foreach (var me in siteModules)
            {
                var pluginAssembly = me.Loader.LoadDefaultAssembly();
                var apm            = HttpContext.RequestServices.GetService <ApplicationPartManager>();
                var partFactory    = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);
                foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
                {
                    Debug.WriteLine($"* {part.Name}");
                    apm.ApplicationParts.Add(part);
                }

                QrnActionDescriptorChangeProvider.Instance.HasChanged = true;
                QrnActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
            }

            return(Content("0"));
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            var rootDir = new DirectoryInfo(_hostingEnvironment.ContentRootPath).Parent;
            var dir     = new DirectoryInfo(Path.Combine(rootDir.ToString(), "Plugin1", "bin", "Debug", "netstandard2.0"));

            foreach (var file in dir.GetFileSystemInfos("*.dll", SearchOption.AllDirectories))
            {
                Assembly assembly;
                try
                {
                    assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
                }
                catch (FileLoadException)
                {
                    // Get loaded assembly. This assembly might be loaded
                    assembly = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(file.Name)));

                    if (assembly == null)
                    {
                        throw;
                    }
                }
            }

            services.Configure <RazorViewEngineOptions>(
                options => { options.ViewLocationExpanders.Add(new ThemeableViewLocationExpander()); });

            var mvcBuilder = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            mvcBuilder.AddRazorOptions(o =>
            {
                o.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Plugin1")).Location));
            });

            var pluginAssembly = Assembly.Load(new AssemblyName("Plugin1"));
            var partFactory    = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                mvcBuilder.PartManager.ApplicationParts.Add(part);
            }

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(pluginAssembly, throwOnError: true);

            foreach (var assembly in relatedAssemblies)
            {
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }
            }
        }
Example #6
0
        public void LoadModules(string path)
        {
            var loadedFolderAssemblies = new List <Assembly>();

            foreach (var dir in Directory.GetDirectories(path))
            {
                string dirPath = dir;
                foreach (var file in Directory.EnumerateFiles(dirPath, "*.dll"))
                {
                    using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var assembly = AssemblyLoadContext.Default.LoadFromStream(fs);
                        loadedFolderAssemblies.Add(assembly);

                        var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                        foreach (var part in partFactory.GetApplicationParts(assembly))
                        {
                            _applicationPartManager.ApplicationParts.Add(part);
                        }
                    }
                }
                if (loadedFolderAssemblies.Any())
                {
                    _loadedAssemblies[dirPath] = loadedFolderAssemblies;
                    _onDemandActionDescriptorChangeProvider.TokenSource.Cancel();
                }
            }
        }
        public static void AddApplicationPartsFromObjectAssembly(object objectAssembly, ApplicationPartManager partsManager)
        {
            var type = objectAssembly.GetType();

            if (!typeof(Service).IsAssignableFrom(type))
            {
                return;
            }

            bool needAddAppPart;

            do
            {
                var appFac = ApplicationPartFactory.GetApplicationPartFactory(type.Assembly);
                var parts  = appFac.GetApplicationParts(type.Assembly);

                foreach (var part in parts)
                {
                    partsManager.ApplicationParts.Add(part);
                }

                type = type.BaseType;

                needAddAppPart = type != typeof(Service);
            }while (needAddAppPart);
        }
Example #8
0
        public static IMvcBuilder AddPlugin <TPlugin>(this IMvcBuilder builder, PluginLoader pluginLoader, Action <TPlugin> configurator = null)
            where TPlugin : class
        {
            var pluginAssembly = pluginLoader.LoadDefaultAssembly();

            foreach (var type in pluginAssembly.GetTypes().Where(x => typeof(TPlugin).IsAssignableFrom(x)))
            {
                var plugin = (TPlugin)Activator.CreateInstance(type);
                configurator?.Invoke(plugin);
                builder.Services.AddSingleton(plugin);
            }

            // This loads MVC application parts from plugin assemblies
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                builder.PartManager.ApplicationParts.Add(part);
            }

            // This piece finds and loads related parts, such as MvcAppPlugin1.Views.dll.
            var relatedAssembliesAttrs = pluginAssembly.GetCustomAttributes <RelatedAssemblyAttribute>();

            foreach (var attr in relatedAssembliesAttrs)
            {
                var assembly = pluginLoader.LoadAssembly(attr.AssemblyFileName);
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    builder.PartManager.ApplicationParts.Add(part);
                }
            }

            return(builder);
        }
        /// <summary>
        /// Loads
        /// </summary>
        /// <param name="mvcBuilder">The MVC builder</param>
        /// <param name="assemblyFile">Full path the main .dll file for the plugin.</param>
        /// <returns>The builder</returns>
        public static IMvcBuilder AddPluginFromAssemblyFile(this IMvcBuilder mvcBuilder, string assemblyFile)
        {
            var plugin = PluginLoader.CreateFromAssemblyFile(
                assemblyFile, // create a plugin from for the .dll file
                config =>
                // this ensures that the version of MVC is shared between this app and the plugin
                config.PreferSharedTypes = true);

            var pluginAssembly = plugin.LoadDefaultAssembly();

            // This loads MVC application parts from plugin assemblies
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                mvcBuilder.PartManager.ApplicationParts.Add(part);
            }

            // This piece finds and loads related parts, such as MvcAppPlugin1.Views.dll.
            var relatedAssembliesAttrs = pluginAssembly.GetCustomAttributes <RelatedAssemblyAttribute>();

            foreach (var attr in relatedAssembliesAttrs)
            {
                var assembly = plugin.LoadAssembly(attr.AssemblyFileName);
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }
            }

            return(mvcBuilder);
        }
        private static void AddToAppcationPart(ApplicationPartManager manager, Assembly assembly)
        {
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            var applicationParts = partFactory.GetApplicationParts(assembly);

            foreach (var part in applicationParts)
            {
                if (part is AssemblyPart assemblyPart)
                {
                    manager.ApplicationParts.Add(new WidgetAssemblyPart(assembly));
                }
                else if (part is CompiledRazorAssemblyPart)
                {
                    manager.ApplicationParts.Add(new WidgetRazorAssemblyPart(new CompiledRazorAssemblyPart(assembly)));
                }
                else
                {
                    manager.ApplicationParts.Add(part);
                }
            }

            // load related assemblies
            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, true);

            foreach (var item in relatedAssemblies)
            {
                AddToAppcationPart(manager, item);
            }
        }
Example #11
0
        /// <summary>
        /// Loads controllers and razor pages from a plugin loader.
        /// <para>
        /// In order for this to work, the PluginLoader instance must be configured to share the types
        /// <see cref="ProvideApplicationPartFactoryAttribute" /> and <see cref="RelatedAssemblyAttribute" />
        /// (comes from Microsoft.AspNetCore.Mvc.Core.dll). The easiest way to ensure that is done correctly
        /// is to set <see cref="PluginConfig.PreferSharedTypes" /> to <c>true</c>.
        /// </para>
        /// </summary>
        /// <param name="mvcBuilder">The MVC builder</param>
        /// <param name="pluginLoader">An instance of PluginLoader.</param>
        /// <returns>The builder</returns>
        public static IMvcBuilder AddPluginLoader(this IMvcBuilder mvcBuilder, PluginLoader pluginLoader)
        {
            var pluginAssembly = pluginLoader.LoadDefaultAssembly();

            // This loads MVC application parts from plugin assemblies
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                mvcBuilder.PartManager.ApplicationParts.Add(part);
            }

            // This piece finds and loads related parts, such as MvcAppPlugin1.Views.dll.
            var relatedAssembliesAttrs = pluginAssembly.GetCustomAttributes <RelatedAssemblyAttribute>();

            foreach (var attr in relatedAssembliesAttrs)
            {
                var assembly = pluginLoader.LoadAssembly(attr.AssemblyFileName);
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }
            }

            return(mvcBuilder);
        }
        private static void AddApplicationPart(IMvcBuilder mvcBuilder, Assembly assembly)
        {
            try
            {
                var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }

                var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: false);
                foreach (var relatedAssembly in relatedAssemblies)
                {
                    partFactory = ApplicationPartFactory.GetApplicationPartFactory(relatedAssembly);
                    foreach (var part in partFactory.GetApplicationParts(relatedAssembly))
                    {
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
Example #13
0
        public static IMvcBuilder AddOqtaneApplicationParts(this IMvcBuilder mvcBuilder)
        {
            if (mvcBuilder is null)
            {
                throw new ArgumentNullException(nameof(mvcBuilder));
            }

            // load MVC application parts from module assemblies
            var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();

            foreach (var assembly in assemblies)
            {
                // check if assembly contains MVC Controllers
                if (assembly.GetTypes().Any(t => t.IsSubclassOf(typeof(Controller)) || t.IsSubclassOf(typeof(Page))))
                {
                    var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                    foreach (var part in partFactory.GetApplicationParts(assembly))
                    {
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }
            }

            return(mvcBuilder);
        }
Example #14
0
        public static IMvcBuilder ConfigureAsRazorModule(this IMvcBuilder builder)
        {
            var frame  = new StackFrame(1);
            var method = frame.GetMethod();
            var type   = method.DeclaringType;

            if (type == null)
            {
                return(builder);
            }

            var moduleAssembly = type.Assembly;

            builder.ConfigureApplicationPartManager(apm =>
            {
                apm.ApplicationParts.Add(new AssemblyPart(moduleAssembly));

                var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(moduleAssembly, false);
                foreach (var relatedAssembly in relatedAssemblies)
                {
                    var partFactory = ApplicationPartFactory.GetApplicationPartFactory(relatedAssembly);
                    foreach (var part in partFactory.GetApplicationParts(relatedAssembly))
                    {
                        apm.ApplicationParts.Add(part);
                    }
                }
            });
            return(builder);
        }
Example #15
0
        private static void AddApplicationPart(ref IMvcBuilder mvcBuilder, Assembly assembly)
        {
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (var part in partFactory.GetApplicationParts(assembly))
            {
                mvcBuilder.PartManager.ApplicationParts.Add(part);
            }
        }
Example #16
0
        public static IMvcBuilder AddPluginFutures(this IMvcBuilder mvcBuilder, IHostEnvironment environment)
        {
            var pluginContext = new PluginContext();

            var pluginsFolder = Path.Combine(environment.ContentRootPath, pluginFolderName);
            foreach (var dir in Directory.GetDirectories(pluginsFolder))
            {
                var pluginFolder = Path.GetFileName(dir);
                var pluginAssemblyPath = Path.Combine(dir, pluginFolder + assemblyFileExtension);
                if (!File.Exists(pluginAssemblyPath))
                    continue;

                var pluginManifestFile = Path.Combine(dir, pluginManifestName);
                if (!File.Exists(pluginManifestFile))
                    continue;

                PluginInfo pluginInfo = null;
                using (var reader = new StreamReader(pluginManifestFile))
                {
                    string content = reader.ReadToEnd();
                    pluginInfo = JsonSerializer.Deserialize<PluginInfo>(content);
                }

                if (pluginInfo == null)
                    continue;

                var pluginLoadContext = new PluginLoadContext(pluginAssemblyPath);

                pluginInfo.Assembly = pluginLoadContext.LoadDefaultAssembly();
                pluginContext.Plugins.Add(pluginInfo);

                var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginInfo.Assembly);
                foreach (var part in partFactory.GetApplicationParts(pluginInfo.Assembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }

                // This piece finds and loads related parts, such as WebPlugin1.Views.dll
                var relatedAssembliesAttrs = pluginInfo.Assembly.GetCustomAttributes<RelatedAssemblyAttribute>();
                foreach (var attr in relatedAssembliesAttrs)
                {
                    var assembly = pluginLoadContext.LoadFromAssemblyName(new AssemblyName(attr.AssemblyFileName));
                    partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                    foreach (var part in partFactory.GetApplicationParts(assembly))
                    {
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }
                }
            }

            mvcBuilder.Services.AddSingleton(pluginContext);

            return mvcBuilder;
        }
Example #17
0
        private static void AddModuleWithApplicationParts(IServiceCollection services, ApplicationPartManager manager,
                                                          Assembly assembly)
        {
            AddModule(services, assembly);

            // add application parts of given assembly into the part manager
            foreach (var applicationPart in ApplicationPartFactory.GetApplicationPartFactory(assembly)
                     .GetApplicationParts(assembly))
            {
                manager.ApplicationParts.Add(applicationPart);
            }
        }
Example #18
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     services
     .AddMvc()
     .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
     .ConfigureApplicationPartManager(partManager =>
     {
         var assembly    = typeof(Startup).Assembly;
         var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
         foreach (var part in partFactory.GetApplicationParts(assembly))
         {
             partManager.ApplicationParts.Add(part);
         }
     });
 }
Example #19
0
        private static void AddParts(IList <ApplicationPart> applicationParts, Assembly assembly)
        {
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (var part in partFactory.GetApplicationParts(assembly))
            {
                var existings = applicationParts.Where(x => x.Name == part.Name).ToList();

                foreach (var existing in existings)
                {
                    applicationParts.Remove(existing);
                }

                applicationParts.Add(part);
            }
        }
Example #20
0
        // This is NOT thread safe but it is only called from within a lock
        private Assembly ReloadAssembly(string pathToAssembly)
        {
            // If there's a current AssemblyLoadContext, unload it before creating a new one.
            if (!(_currentAssemblyLoadContext is null))
            {
                _currentAssemblyLoadContext.Unload();

                // we need to remove the current part too
                ApplicationPart currentPart = _applicationPartManager.ApplicationParts.FirstOrDefault(x => x.Name == RoslynCompiler.GeneratedAssemblyName);
                if (currentPart != null)
                {
                    _applicationPartManager.ApplicationParts.Remove(currentPart);
                }
            }

            // We must create a new assembly load context
            // as long as theres a reference to the assembly load context we can't delete the assembly it loaded
            _currentAssemblyLoadContext = new UmbracoAssemblyLoadContext();

            // NOTE: We cannot use in-memory assemblies due to the way the razor engine works which must use
            // application parts in order to add references to it's own CSharpCompiler.
            // These parts must have real paths since that is how the references are loaded. In that
            // case we'll need to work on temp files so that the assembly isn't locked.

            // Get a temp file path
            // NOTE: We cannot use Path.GetTempFileName(), see this issue:
            // https://github.com/dotnet/AspNetCore.Docs/issues/3589 which can cause issues, this is recommended instead
            var tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            File.Copy(pathToAssembly, tempFile, true);

            // Load it in
            Assembly assembly = _currentAssemblyLoadContext.LoadFromAssemblyPath(tempFile);

            // Add the assembly to the application parts - this is required because this is how
            // the razor ReferenceManager resolves what to load, see
            // https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Razor.RuntimeCompilation/src/RazorReferenceManager.cs#L53
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (ApplicationPart applicationPart in partFactory.GetApplicationParts(assembly))
            {
                _applicationPartManager.ApplicationParts.Add(applicationPart);
            }

            return(assembly);
        }
Example #21
0
        public void ConfigureServices(IServiceCollection services)
        {
            var pluginWithControllerAssemblies = Program
                                                 .PluginLoader
                                                 .ControllerTypes
                                                 .Select(x => x.Assembly)
                                                 .Distinct()
                                                 .ToArray();

            services
            .AddMvc()
            .ConfigureApplicationPartManager(manager =>
            {
                foreach (var assembly in pluginWithControllerAssemblies)
                {
                    var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                    foreach (var applicationPart in partFactory.GetApplicationParts(assembly))
                    {
                        manager.ApplicationParts.Add(applicationPart);
                    }
                }
            });
            services.AddControllers().AddNewtonsoftJson();
            services.AddResponseCompression(options =>
            {
                options.Providers.Add <GzipCompressionProvider>();
            });
            services.Configure <GzipCompressionProviderOptions>(conf => conf.Level = System.IO.Compression.CompressionLevel.Optimal);
            services.AddResponseCompression();
            services.AddHttpClient();

            services.AddSwaggerGen(configuration =>
            {
                // Try get all documentation .xml files from plugins with controllers
                foreach (var pluginWithControllerAssembly in pluginWithControllerAssemblies)
                {
                    // Check plugin exists
                    var fileInfo = new FileInfo(pluginWithControllerAssembly.Location);
                    if (!fileInfo.Exists)
                    {
                        continue;
                    }

                    // Check documentation file exists
                    var file = fileInfo.FullName;
                    var docu = new FileInfo($"{file[..file.LastIndexOf(fileInfo.Extension)]}.xml");
 public static IMvcBuilder AddModuleAssemblies(this IMvcBuilder mvcBuilder, List <Assembly> assemblies)
 {
     // load MVC application parts from module assemblies
     foreach (Assembly assembly in assemblies)
     {
         // check if assembly contains MVC Controllers
         if (assembly.GetTypes().Where(item => item.IsSubclassOf(typeof(Controller))).ToArray().Length > 0)
         {
             var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
             foreach (var part in partFactory.GetApplicationParts(assembly))
             {
                 mvcBuilder.PartManager.ApplicationParts.Add(part);
             }
         }
     }
     return(mvcBuilder);
 }
        private static void AddApplicationParts(ref List <ApplicationPart> applicationParts, Assembly assembly)
        {
            var applicationPartFactory   = ApplicationPartFactory.GetApplicationPartFactory(assembly);
            var assemblyApplicationParts = applicationPartFactory.GetApplicationParts(assembly);

            foreach (var assemblyApplicationPart in assemblyApplicationParts)
            {
                if (!applicationParts.Any(applicationPart => applicationPart.Name == assemblyApplicationPart.Name))
                {
                    applicationParts.Add(assemblyApplicationPart);
                }
                else
                {
                    Logger.Log($"ApplicationPart {assemblyApplicationPart.Name} already added");
                }
            }
        }
Example #24
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var mvcBuilder = services
                             .AddMvc()
                             .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);

            services.AddSingleton <IConfiguration>(Configuration);

            services.AddHttpClient();

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(o => o.LoginPath = new PathString("/account/signin"));

            // Load and install modules. Get all module folder names inside 'Modules' folder
            if (Directory.Exists(Path.Combine(AppContext.BaseDirectory, "modules")))
            {
                foreach (var dir in Directory.GetDirectories(Path.Combine(AppContext.BaseDirectory, "modules")))
                {
                    var moduleAssembly = new CustomAssemblyLoadContext().LoadFromAssemblyPath(Path.Combine(dir, Path.GetFileName(dir)) + ".dll");
                    Console.WriteLine($"Loading application parts from module {moduleAssembly.FullName}");

                    // This loads MVC application parts from module assemblies
                    var partFactory = ApplicationPartFactory.GetApplicationPartFactory(moduleAssembly);
                    foreach (var part in partFactory.GetApplicationParts(moduleAssembly))
                    {
                        Console.WriteLine($"* {part.Name}");
                        mvcBuilder.PartManager.ApplicationParts.Add(part);
                    }

                    // This piece finds and loads related parts, such as SampleModule.Views.dll.
                    var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(moduleAssembly, throwOnError: true);
                    foreach (var assembly in relatedAssemblies)
                    {
                        partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                        foreach (var part in partFactory.GetApplicationParts(assembly))
                        {
                            Console.WriteLine($"  * {part.Name}");
                            mvcBuilder.PartManager.ApplicationParts.Add(part);
                        }
                    }
                }
            }
        }
Example #25
0
        public override void ConfigureServices(IServiceCollection services)
        {
            var manager    = new ApplicationPartManager();
            var assemblies = new Assembly[] { Assembly.GetExecutingAssembly(), Assembly.Load("UwpAspNetLib"), Assembly.Load("UwpAspNetLib.Views") };

            foreach (var assembly in assemblies)
            {
                var factory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

                foreach (var part in factory.GetApplicationParts(assembly))
                {
                    manager.ApplicationParts.Add(part);
                }
            }

            services.AddSingleton(manager);

            base.ConfigureServices(services);
        }
Example #26
0
        private static void AddApplicationPart(IMvcBuilder mvcBuilder, Assembly assembly)
        {
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (var part in partFactory.GetApplicationParts(assembly))
            {
                mvcBuilder.PartManager.ApplicationParts.Add(part);
            }

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, false);

            foreach (var relatedAssembly in relatedAssemblies)
            {
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(relatedAssembly);
                foreach (var part in partFactory.GetApplicationParts(relatedAssembly))
                {
                    mvcBuilder.PartManager.ApplicationParts.Add(part);
                }
            }
        }
        private static void AddApplicationPart(ApplicationPartManager applicationPartManager, Assembly assembly)
        {
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (var part in partFactory.GetApplicationParts(assembly))
            {
                applicationPartManager.ApplicationParts.Add(part);
            }

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, throwOnError: true);

            foreach (var relatedAssembly in relatedAssemblies)
            {
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(relatedAssembly);
                foreach (var part in partFactory.GetApplicationParts(relatedAssembly))
                {
                    applicationPartManager.ApplicationParts.Add(part);
                }
            }
        }
Example #28
0
        private void ConfigurePlugin(ApplicationPartManager applicationPartManager)
        {
            var pluginAssembly = typeof(Startup).Assembly;
            var partFactory    = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                applicationPartManager.ApplicationParts.Add(part);
            }

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(pluginAssembly, throwOnError: true);

            foreach (var assembly in relatedAssemblies)
            {
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    applicationPartManager.ApplicationParts.Add(part);
                }
            }
        }
        private static void PopulateCustomParts(this ApplicationPartManager manager, string entryAssemblyName)
        {
            var entryAssembly = Assembly.Load(new AssemblyName(entryAssemblyName));
            //var assembliesProvider = new ApplicationAssembliesProvider();
            //var applicationAssemblies = assembliesProvider.ResolveAssemblies(entryAssembly);//获取对应视图assembly

            //foreach (var assembly in applicationAssemblies)
            //{
            var assembly    = entryAssembly;
            var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);

            foreach (var part in partFactory.GetApplicationParts(assembly))
            {
                if (!manager.ApplicationParts.Contains(part))
                {
                    manager.ApplicationParts.Add(part);
                }
                ;
            }
            //}
        }
Example #30
0
        private void ConfigurePlugin(ApplicationPartManager applicationPartManager)
        {
            var pluginAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(Directory.GetCurrentDirectory(), "PluginApp", "PluginApp.dll"));
            var partFactory    = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);

            foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
            {
                applicationPartManager.ApplicationParts.Add(part);
            }

            var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(pluginAssembly, throwOnError: true);

            foreach (var assembly in relatedAssemblies)
            {
                partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
                foreach (var part in partFactory.GetApplicationParts(assembly))
                {
                    applicationPartManager.ApplicationParts.Add(part);
                }
            }
        }