public AppDomain createAppDomain(string appDomainName, AppDomainSetup appDomainSetup) { try { if (AppDomains_ControledByO2Kernel.ContainsKey(appDomainName)) { PublicDI.log.error("in createAppDomain, appDomainName provided has already been used, appDomainNames must be unique: {0}", appDomainName); } else { PublicDI.log.info("Creating AppDomain {0} with Base Directory {1}", appDomainName, appDomainSetup.ApplicationBase); // ensure target directory exits O2Kernel_Files.checkIfDirectoryExistsAndCreateIfNot(appDomainSetup.ApplicationBase); // give our appDomain full trust :) var permissionSet = new PermissionSet(PermissionState.Unrestricted); AppDomains_ControledByO2Kernel.Add(appDomainName, this); //Create domain AppDomain = AppDomain.CreateDomain(appDomainName, null, appDomainSetup, permissionSet); // appDomain.AssemblyResolve += new ResolveEventHandler(assemblyResolve); BaseDirectory = AppDomain.BaseDirectory; AppDomain.DomainUnload += (sender, e) => this.removeDomainFromManagedList(); return(AppDomain); } } catch (Exception ex) { PublicDI.log.ex(ex, "could not load createAppDomain: " + appDomainName); } return(null); }
public string downloadBinaryFile_Action(string urlOfFileToFetch, string targetFileOrFolder) { var targetFile = targetFileOrFolder; if (Directory.Exists(targetFileOrFolder)) { targetFile = targetFileOrFolder.pathCombine(urlOfFileToFetch.fileName()); } PublicDI.log.debug("Downloading Binary File {0}", urlOfFileToFetch); lock (this) { using (var webClient = new WebClient()) { try { byte[] pageData = webClient.DownloadData(urlOfFileToFetch); O2Kernel_Files.WriteFileContent(targetFile, pageData); PublicDI.log.debug("Downloaded File saved to: {0}", targetFile); webClient.Dispose(); GC.Collect(); // because of WebClient().GetRequestStream prob return(targetFile); } catch (Exception ex) { PublicDI.log.ex(ex); } } } GC.Collect(); // because of WebClient().GetRequestStream prob return(null); }
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 { 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) { // last change load assembly into current appdomain to get its full name and try again try { AppDomain.Load(fullAssemblyName.assembly().FullName); } catch (Exception ex3) { 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); return(false); } } } return(true); }
/*static Assembly assemblyResolve(object sender, ResolveEventArgs args) * { * * //return Assembly.LoadFile(GetAssemblyFileName()); * return null; * } */ public void loadAssembliesIntoAppDomain(List <string> assembliesToLoad) { var assembliesInNewAppDomain = new List <String>(); // first copy the assemblies foreach (var assemblyToLoad in assembliesToLoad) { if (Path.GetExtension(assemblyToLoad).ToLower() == ".dll" || Path.GetExtension(assemblyToLoad).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); } } }