public static void UnloadDomain(String name) { if (m_domains.ContainsKey(name)) { AppDomain domainToUnload = m_domains[name]; if (!domainToUnload.Equals(AppDomain.CurrentDomain)) { System.AppDomain.Unload(domainToUnload); m_domains.Remove(name); } } }
// This handler is called in main AppDomain public static void TcPluginCallbackHandler() { AppDomain domain = AppDomain.CurrentDomain; string pluginId; try { pluginId = (string)domain.GetData(PluginCallbackDataName); } finally { domain.SetData(PluginCallbackDataName, null); } TcPlugin tp = TcPluginLoader.GetTcPluginById(pluginId); if (tp == null) { throw new InvalidOperationException(ErrorMsg1); } if (!domain.Equals(tp.MainDomain)) { throw new InvalidOperationException(ErrorMsg2); } #if TRACE writeTrace = tp.WriteTrace; #endif string callbackDataBufferName = tp.DataBufferName; try { object o = domain.GetData(callbackDataBufferName); if (o == null || !(o is PluginEventArgs)) { throw new ArgumentException(ErrorMsg3); } HandleTcPluginEvent(tp, o as PluginEventArgs); } finally { #if TRACE writeTrace = false; #endif } }
public static int Main(string[] args) { AppDomain cd = AppDomain.CurrentDomain; AppDomain executionAD = cd; string nantShadowCopyFilesSetting = ConfigurationSettings.AppSettings.Get("nant.shadowfiles"); string nantCleanupShadowCopyFilesSetting = ConfigurationSettings.AppSettings.Get("nant.shadowfiles.cleanup"); if (FrameworkVersion == null) { // signal error return(1); } string frameworkFamilyLibDir = Path.Combine("lib", FrameworkFamily); string frameworkVersionLibDir = Path.Combine(frameworkFamilyLibDir, FrameworkVersion); string privateBinPath = null; // add lib/<family>/<version> dir to privatebinpath if it exists if (Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkVersionLibDir))) { privateBinPath += (privateBinPath != null) ? Path.PathSeparator + frameworkVersionLibDir : frameworkVersionLibDir; } // add lib/<family> dir to privatebinpath if it exists if (Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, frameworkFamilyLibDir))) { privateBinPath += (privateBinPath != null) ? Path.PathSeparator + frameworkFamilyLibDir : frameworkFamilyLibDir; } // add privatebinpath of current domain to privatebinpath if (AppDomain.CurrentDomain.SetupInformation.PrivateBinPath != null) { privateBinPath += Path.PathSeparator + AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; } if (nantShadowCopyFilesSetting != null && bool.Parse(nantShadowCopyFilesSetting) == true) { logger.Debug(string.Format( CultureInfo.InvariantCulture, "Shadowing files({0}) -- cleanup={1}", nantShadowCopyFilesSetting, nantCleanupShadowCopyFilesSetting)); System.AppDomainSetup myDomainSetup = new System.AppDomainSetup(); myDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; logger.Debug(string.Format( CultureInfo.InvariantCulture, "NAntDomain.PrivateBinPath={0}", myDomainSetup.PrivateBinPath)); myDomainSetup.PrivateBinPath = privateBinPath; myDomainSetup.ApplicationName = "NAnt"; // copy the config file location myDomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; logger.Debug(string.Format( CultureInfo.InvariantCulture, "NAntDomain.ConfigurationFile={0}", myDomainSetup.ConfigurationFile)); // yes, cache the files myDomainSetup.ShadowCopyFiles = "true"; // shadowcopy everything in base directory of appdomain and // privatebinpath myDomainSetup.ShadowCopyDirectories = myDomainSetup.ApplicationBase + Path.PathSeparator + myDomainSetup.PrivateBinPath; logger.Debug(string.Format( CultureInfo.InvariantCulture, "NAntDomain.ShadowCopyDirectories={0}", myDomainSetup.ShadowCopyDirectories)); // try to cache in .\cache folder, if that fails, let the system // figure it out. string cachePath = Path.Combine(myDomainSetup.ApplicationBase, "cache"); DirectoryInfo cachePathInfo = null; try { cachePathInfo = Directory.CreateDirectory(cachePath); } catch (Exception e) { System.Console.WriteLine("Failed to create: {0}. Using default CachePath." + e.ToString(), cachePath); } finally { if (cachePathInfo != null) { myDomainSetup.CachePath = cachePathInfo.FullName; } logger.Debug(string.Format( CultureInfo.InvariantCulture, "NAntDomain.CachePath={0}", myDomainSetup.CachePath)); } // create the domain. executionAD = AppDomain.CreateDomain(myDomainSetup.ApplicationName, AppDomain.CurrentDomain.Evidence, myDomainSetup); logger.Debug(string.Format( CultureInfo.InvariantCulture, "NAntDomain.SetupInfo:\n{0}", executionAD.SetupInformation)); } // use helper object to hold (and serialize) args for callback. logger.Debug(string.Format( CultureInfo.InvariantCulture, "Creating HelperArgs({0})", args.ToString())); HelperArguments helper = new HelperArguments(args, privateBinPath); executionAD.DoCallBack(new CrossAppDomainDelegate(helper.CallConsoleRunner)); // unload if remote/new appdomain if (!cd.Equals(executionAD)) { string cachePath = executionAD.SetupInformation.CachePath; logger.Debug(string.Format( CultureInfo.InvariantCulture, "Unloading '{0}' AppDomain", executionAD.FriendlyName)); AppDomain.Unload(executionAD); if (nantCleanupShadowCopyFilesSetting != null && bool.Parse(nantCleanupShadowCopyFilesSetting) == true) { logger.Debug(string.Format( CultureInfo.InvariantCulture, "Unloading '{0}' AppDomain", executionAD.FriendlyName)); try { logger.Debug(string.Format( CultureInfo.InvariantCulture, "Cleaning up CacheFiles in '{0}'", cachePath)); Directory.Delete(cachePath, true); } catch (FileNotFoundException ex) { logger.Error("Files not found.", ex); } catch (Exception ex) { System.Console.WriteLine("Unable to delete cache path '{1}'.\n\n{0}.", ex.ToString(), cachePath); } } } if (helper == null || helper.ExitCode == -1) { logger.Debug(string.Format( CultureInfo.InvariantCulture, "Return Code null or -1")); throw new ApplicationException("No return code set!"); } else { logger.Debug(string.Format( CultureInfo.InvariantCulture, "Return Code = {0}", helper.ExitCode)); return(helper.ExitCode); } }