Example #1
0
        /// <summary>
        /// To be used with Razor class libraries loaded dynamically
        /// </summary>
        void LoadDynamicLibraries(ApplicationPartManager PartManager)
        {
            // get the output folder of this application
            string moduleSource = Configuration["ModulesPath"];

            moduleSource = Path.GetDirectoryName(moduleSource);

            // get the full filepath of any dll starting with the rcl_ prefix
            string Prefix        = "Mod_";
            string SearchPattern = $"{Prefix}*.dll";

            string[] LibraryPaths = Directory.GetFiles(moduleSource, SearchPattern);

            if (LibraryPaths != null && LibraryPaths.Length > 0)
            {
                // create the load context
                LibraryLoadContext LoadContext = new LibraryLoadContext(moduleSource);

                Assembly        Assembly;
                ApplicationPart ApplicationPart;
                foreach (string LibraryPath in LibraryPaths)
                {
                    // load each assembly using its filepath
                    Assembly = LoadContext.LoadFromAssemblyPath(LibraryPath);

                    // create an application part for that assembly
                    ApplicationPart = LibraryPath.EndsWith(".Views.dll") ? new CompiledRazorAssemblyPart(Assembly) as ApplicationPart : new AssemblyPart(Assembly);

                    // register the application part
                    PartManager.ApplicationParts.Add(ApplicationPart);

                    // if it is NOT the *.Views.dll add it to a list for later use
                    if (!LibraryPath.EndsWith(".Views.dll"))
                    {
                        _dynamicallyLoadedLibraries.Add(Assembly);
                    }
                }
            }
        }