Example #1
0
        /// <summary>
        /// Make sure the given assemblies are loaded and that their loadable classes have been catalogued.
        /// </summary>
        public static void CacheClassesExtra(string[] assemblies)
        {
            if (Utils.Size(assemblies) > 0)
            {
                lock (_lock)
                {
                    foreach (string path in assemblies)
                    {
                        if (!_cachedPaths.Add(path))
                        {
                            continue;
                        }

                        Exception ex = null;
                        try
                        {
                            // REVIEW: Will LoadFrom ever return null?
                            var assem = LoadFrom(path);
                            if (assem != null)
                            {
                                continue;
                            }
                        }
                        catch (Exception e)
                        {
                            ex = e;
                        }

                        // If it is a zip file, load it that way.
                        ZipArchive zip;
                        try
                        {
                            zip = ZipFile.OpenRead(path);
                        }
                        catch (Exception e)
                        {
                            // Couldn't load as an assembly and not a zip, so warn the user.
                            ex = ex ?? e;
                            Console.Error.WriteLine("Warning: Could not load '{0}': {1}", path, ex.Message);
                            continue;
                        }

                        string dir;
                        try
                        {
                            dir = CreateTempDirectory();
                        }
                        catch (Exception e)
                        {
                            throw Contracts.ExceptIO(e, "Creating temp directory for extra assembly zip extraction failed: '{0}'", path);
                        }

                        try
                        {
                            zip.ExtractToDirectory(dir);
                        }
                        catch (Exception e)
                        {
                            throw Contracts.ExceptIO(e, "Extracting extra assembly zip failed: '{0}'", path);
                        }

                        LoadAssembliesInDir(dir, false);
                    }
                }
            }

            CacheLoadedAssemblies();
        }
Example #2
0
        /// <summary>
        /// Make sure the given assemblies are loaded and that their loadable classes have been catalogued.
        /// </summary>
        public static void LoadAndRegister(IHostEnvironment env, string[] assemblies)
        {
            Contracts.AssertValue(env);

            if (Utils.Size(assemblies) > 0)
            {
                foreach (string path in assemblies)
                {
                    Exception ex = null;
                    try
                    {
                        // REVIEW: Will LoadFrom ever return null?
                        Contracts.CheckNonEmpty(path, nameof(path));
                        var assem = LoadAssembly(env, path);
                        if (assem != null)
                        {
                            continue;
                        }
                    }
                    catch (Exception e)
                    {
                        ex = e;
                    }

                    // If it is a zip file, load it that way.
                    ZipArchive zip;
                    try
                    {
                        zip = ZipFile.OpenRead(path);
                    }
                    catch (Exception e)
                    {
                        // Couldn't load as an assembly and not a zip, so warn the user.
                        ex = ex ?? e;
                        Console.Error.WriteLine("Warning: Could not load '{0}': {1}", path, ex.Message);
                        continue;
                    }

                    string dir;
                    try
                    {
                        dir = CreateTempDirectory();
                    }
                    catch (Exception e)
                    {
                        throw Contracts.ExceptIO(e, "Creating temp directory for extra assembly zip extraction failed: '{0}'", path);
                    }

                    try
                    {
                        zip.ExtractToDirectory(dir);
                    }
                    catch (Exception e)
                    {
                        throw Contracts.ExceptIO(e, "Extracting extra assembly zip failed: '{0}'", path);
                    }

                    LoadAssembliesInDir(env, dir, false);
                }
            }
        }