示例#1
0
        private PluginInfo[] LoadPlugins()
        {
            var pluginFolder             = GetPluginFolder();
            var pluginFolderSettingsName = $"{nameof(Plugin)}:{nameof(Plugin.Folder)}";

            if (string.IsNullOrWhiteSpace(pluginFolder))
            {
                _logger.Info($"'{pluginFolderSettingsName}' setting not configured. No plugins will be loaded.");
                return(new PluginInfo[0]);
            }

            if (!Directory.Exists(pluginFolder))
            {
                _logger.Warn($"Plugin folder '{pluginFolder}' does not exist. No plugins will be loaded.");

                _logger.Warn(
                    $"To configure plugins update the '{pluginFolderSettingsName}' setting with either an absolute path, a path relative to the 'Ed-Fi-ODS-Implementation\\Application\\EdFi.Ods.WebApi\', or a path relative to the deployed EdFi.Ods.WebApi executable.");

                return(new PluginInfo[0]);
            }

            try
            {
                _logger.Info($"Loading plugins from: '{pluginFolder}'");
                var assemblyFiles = AssemblyLoaderHelper.FindPluginAssemblies(pluginFolder);

                // IMPORTANT: Load the plug-in assembly into the Default context
                return(assemblyFiles
                       .Select(
                           assemblyFile => new PluginInfo
                {
                    AssemblyFileName = assemblyFile,
                    Assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFile)
                })
                       .ToArray());
            }
            finally
            {
                // LoadPluginAssemblies method creates a pluginFinderAssemblyContext and loads assembles in it to
                // determine plugins to load in the app domain. Need to force a garbage collection to unload
                // the pluginFinderAssemblyContext immediately or else assemblies loaded in this context will
                // be in the current app domain.
                GC.Collect();
            }
        }