I have to execute some line which comes from a jscript into another appdomain than the current one. For this i have the following piece of code.
AppDomain ad = null;
try
{
ad = AppDomain.CreateDomain("new AD" + new Random(), null, null);
ad.CreateInstanceAndUnwrap
(
assembly,
type,
true,
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
null,
args,
null,
null,
null
);
}
catch (Exception e)
{
throw new Exception("Automated script engine had an error. " + e.Message + "\n" + e.InnerException.StackTrace + "\n" + e.HelpLink);
}
finally
{
if (ad != null)
{
Assembly[] a = ad.GetAssemblies();
Console.WriteLine(a.Length);
Assembly[] mainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
Console.WriteLine(mainAssemblies.Length);
AppDomain.Unload(ad);
GC.AddMemoryPressure(GC.GetTotalMemory(true));
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.Collect();
ad = null;
}
}
}
But when I inspect all assemblies loaded into the current AppDomain (via AppDomain.CurrentDomain.GetAssemblies()
) my asembly is also loaded.
And as i might have to run let's say 2000 tests
, i understand that 2000 assemblies
will be loaded into my CurrentDomain
.
Is there a way to load this assembly into an AppDomain but without adding it to the CurrentDomain ?
The method calls in the finally
block are just my tries over here. Don't judge me for those if they are not useful maybe.