public void Load_AddsAssemblyToAppDomain() { var data = new AppDomainLoadData(); AppDomainLoader.LoadToAppDomain(data); var loadedAssembliesNames = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name); Assert.True(loadedAssembliesNames.Contains("AssemblyToLoad"), "AppdomainLoader failed to load assembly"); }
private void LoadAppDomain(XNode configRoot) { var mde = configRoot.XPathSelectElement("appdomain"); var deleteShadowOnStartup = mde.Attribute("DeleteShadowDirectoryOnStartup")?.Value.ToBoolean() ?? true; var moduleDirectory = configRoot.XPathSelectElement("appdomain/modules")?.Attribute("Directory")?.Value ?? "ModuleInstances"; var moduleShadowCopyDirectory = configRoot.XPathSelectElement("appdomain/modules")?.Attribute("ShadowCopyDirectory")?.Value ?? @"ModuleInstances\bin"; var pluginsDirectory = configRoot.XPathSelectElement("appdomain/plugins")?.Attribute("Directory")?.Value ?? "Plugins"; var pluginsShadowCopyDirectory = configRoot.XPathSelectElement("appdomain/plugins")?.Attribute("ShadowCopyDirectory")?.Value ?? @"Plugins\bin"; AppDomainLoadData = new AppDomainLoadData(deleteShadowOnStartup, new DynamicLoadingData(pluginsDirectory, pluginsShadowCopyDirectory), new DynamicLoadingData(moduleDirectory, moduleShadowCopyDirectory)); }
/// <summary> /// Loads all components to AppDomain /// </summary> /// <param name="data">AppDomainLoadData <see cref="AppDomainLoadData" /></param> public static void LoadToAppDomain(AppDomainLoadData data) { var componentDirectories = new[] {data.PluginsDynamicLoadingData, data.ModulesDynamicLoadingData}; foreach (var componentDir in componentDirectories) { using (new WriteLockDisposable(Locker)) { var cDirAbsolutePath = componentDir.RootDirectory; Guard.NotEmpty(cDirAbsolutePath); if (!Directory.Exists(cDirAbsolutePath)) continue; var shadowCopyDirectory = componentDir.ShadowCopyDirectory; var binFiles = Directory.Exists(shadowCopyDirectory) ? Directory.GetFiles(shadowCopyDirectory) : new string[] {}; DeleteShadowDirectoryIfRequired(data.DeleteShadowDirectoryOnStartup, shadowCopyDirectory, binFiles); IoUtil.CreateDirectoryIfNotExists(shadowCopyDirectory); try { var dynamicLoadedFiles = Directory.GetFiles(cDirAbsolutePath, "*.dll", SearchOption.AllDirectories) //Not in the shadow copy .Where(x => !binFiles.Contains(x)) .ToArray(); //load all other referenced assemblies now foreach (var dlf in dynamicLoadedFiles .Where(x => !IsAlreadyLoaded(x))) { Guard.NotEmpty(dlf); PerformFileDeploy(new FileInfo(dlf), shadowCopyDirectory); } } catch (ReflectionTypeLoadException ex) { var msg = string.Empty; foreach (var exception in ex.LoaderExceptions) msg = msg + exception.Message + Environment.NewLine; var fail = new Exception(msg, ex); Debug.WriteLine(fail.Message, fail); throw fail; } catch (Exception ex) { var msg = string.Empty; for (var e = ex; e != null; e = e.InnerException) msg += e.Message + Environment.NewLine; var fail = new Exception(msg, ex); Debug.WriteLine(fail.Message, fail); throw fail; } } } }