public static AppDomain loadAssembliesIntoAppDomain(this AppDomain appDomain, List <string> assembliesToLoad) { var assembliesInNewAppDomain = new List <String>(); // first copy the assemblies foreach (var assemblyToLoad in assembliesToLoad) { if (assemblyToLoad.extension(".dll") || assemblyToLoad.extension().ToLower() == ".exe") // since other wise these might be GAC assemblies { if (File.Exists(assemblyToLoad)) { var targetFileName = Path.Combine(appDomain.BaseDirectory, assemblyToLoad.fileName()); if (targetFileName.fileExists().isFalse()) { O2Kernel_Files.Copy(assemblyToLoad, targetFileName); } assembliesInNewAppDomain.Add(targetFileName); } else { var resolvedName = Path.Combine(PublicDI.config.CurrentExecutableDirectory, assemblyToLoad); if (File.Exists(resolvedName)) { var targetFileName = Path.Combine(appDomain.BaseDirectory, resolvedName.fileName()); if (targetFileName.fileExists().isFalse()) { O2Kernel_Files.Copy(resolvedName, targetFileName); } assembliesInNewAppDomain.Add(targetFileName); } else { PublicDI.log.error("in loadAssembliesIntoAppDomain , could not find dll to copy: {0}", assemblyToLoad); } } } } // then load them (and if there are no missing dependencies ALL should load ok foreach (var assemblyToLoad in assembliesInNewAppDomain) { try { appDomain.Load(Path.GetFileNameWithoutExtension(assemblyToLoad)); } catch (Exception ex) { PublicDI.log.ex(ex, "in O2AppDomainFactory.loadAssembliesIntoAppDomain, could not load assembly: " + assemblyToLoad); } } return(appDomain); }
public bool load(string fullAssemblyName, string pathToAssemblyToLoad, bool copyToAppDomainBaseDirectoryBeforeLoad) { try { // copy if asked to if (copyToAppDomainBaseDirectoryBeforeLoad) { try { if (File.Exists(pathToAssemblyToLoad)) { O2Kernel_Files.Copy(pathToAssemblyToLoad, appDomain.BaseDirectory); } else { DI.log.error( "copyToAppDomainBaseDirectoryBeforeLoad was set but pathToAssemblyToLoad was set to a file that didn't exist: {0}", pathToAssemblyToLoad); } } catch (Exception ex) { ex.log("in load copyToAppDomainBaseDirectoryBeforeLoad"); } } // load assembly into AppDomain //First try directly appDomain.Load(fullAssemblyName); } catch (Exception ex1) { //then try using AssemblyName try { appDomain.Load(AssemblyName.GetAssemblyName(fullAssemblyName)); } catch (Exception ex2) { // last change load assembly into current appdomain to get its full name and try again try { appDomain.Load(Kernel.ExtensionMethods.Reflection_ExtensionMethods.assembly(fullAssemblyName).FullName); } catch (Exception ex3) { DI.log.ex(ex1, "could not load assembly (method1): " + fullAssemblyName); DI.log.ex(ex2, "could not load assembly (method2): " + fullAssemblyName); DI.log.ex(ex3, "could not load assembly (method3): " + fullAssemblyName); return(false); } } } return(true); }
public static bool load(this AppDomain appDomain, string fullAssemblyName, string pathToAssemblyToLoad, bool copyToAppDomainBaseDirectoryBeforeLoad) { try { // copy if asked to if (copyToAppDomainBaseDirectoryBeforeLoad) { try { if (File.Exists(pathToAssemblyToLoad)) { O2Kernel_Files.Copy(pathToAssemblyToLoad, appDomain.BaseDirectory); } else { PublicDI.log.error( "copyToAppDomainBaseDirectoryBeforeLoad was set but pathToAssemblyToLoad was set to a file that didn't exist: {0}", pathToAssemblyToLoad); } } catch (Exception ex) { ex.log("in load copyToAppDomainBaseDirectoryBeforeLoad"); } } // load assembly into AppDomain //First try directly appDomain.Load(fullAssemblyName); } catch (Exception ex1) { //then try using AssemblyName try { appDomain.Load(AssemblyName.GetAssemblyName(fullAssemblyName)); } catch (Exception ex2) { // then load assembly into current appdomain to get its full name and try again try { appDomain.Load(fullAssemblyName.assembly().FullName); } catch (Exception ex3) { // last change load assembly into current appdomain to try to use the full path of the assembly (if it is known to the current process) var assemblyLocation = fullAssemblyName.assembly_Location(); if (assemblyLocation.fileExists()) { try { appDomain.Load(assemblyLocation); } catch (Exception ex4) { PublicDI.log.ex(ex1, "could not load assembly (method1): " + fullAssemblyName); PublicDI.log.ex(ex2, "could not load assembly (method2): " + fullAssemblyName); PublicDI.log.ex(ex3, "could not load assembly (method3): " + fullAssemblyName); PublicDI.log.ex(ex4, "could not load assembly (method4): " + assemblyLocation); return(false); } } } } } return(true); }