Beispiel #1
0
        private IEnumerable <IModule> LoadModules(Configuration.Module module, string modulePath)
        {
            try
            {
                _logger.LogInformation($"Loading '{module.Name}' from '{modulePath}'");

                using (_logger.BeginScope($"Loading '{module.Name}' from '{modulePath}'"))
                {
                    var loadContext = new LoadContext($"{modulePath}.dll", module.Name, _logger);

                    var assembly = loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileName(module.Assembly)));

                    return(assembly
                           .GetTypes()
                           .Where(type => typeof(IModule).IsAssignableFrom(type))
                           .Select(Activator.CreateInstance)
                           .Cast <IModule>()
                           .ToArray());
                }
            }
            catch (Exception exception)
            {
                if (module.Optional)
                {
                    _logger.LogWarning($"The module named '{module.Name}' could not be loaded as the assembly '{module.Assembly}' could not be found");

                    return(Enumerable.Empty <IModule>());
                }
                else
                {
                    throw new NotFoundException(module.Name, modulePath, exception);
                }
            }
        }
Beispiel #2
0
        private string GetModulePath(Configuration.Module module)
        {
            var fileName  = Path.GetFileName(module.Assembly);
            var directory = Path.GetDirectoryName(module.Assembly);
            var path      = string.IsNullOrWhiteSpace(directory) ? _workingDirectory : directory;

            return(Path.Combine(path, fileName));
        }