public static void Run(string[] args)
{
// args[0] = process domain assembly path
// args[1] = guid
// args[2] = parent process id
// args[3] = ProcessDomainSetup file
if (args.Length != 4)
{
return;
}
string friendlyName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
string guid = args[1];
int processId = int.Parse(args[2]);
var domainSetup = ProcessDomainSetup.Deserialize(args[3]);
var domain = AppDomain.CreateDomain(friendlyName, domainSetup.Evidence, domainSetup.AppDomainSetupInformation);
var type = Assembly.GetEntryAssembly().GetType("AppSecInc.ProcessDomain.AssemblyResolver");
if (type == null)
{
throw new TypeLoadException("Could not load type for assembly resolver");
}
// add ProcessDomain assembly to resolver
if (domainSetup.ExternalAssemblies == null)
{
domainSetup.ExternalAssemblies = new System.Collections.Generic.Dictionary<AssemblyName, string>();
}
domainSetup.ExternalAssemblies[typeof(ActivatorHost).Assembly.GetName()] = typeof(ActivatorHost).Assembly.Location;
var resolver = domain.CreateInstanceFromAndUnwrap(type.Assembly.Location,
type.FullName,
false,
BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance,
null,
new[] { domainSetup.ExternalAssemblies },
null, null, null);
type.InvokeMember("Setup", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, resolver, null);
var host = (ActivatorHost) domain.CreateInstanceFromAndUnwrap(typeof (ActivatorHost).Assembly.Location,
typeof (ActivatorHost).FullName,
false,
BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance,
null,
new object[] {guid, processId, domainSetup},
null, null, null);
host.WaitForExit();
type.InvokeMember("TearDown", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, resolver, null);
// If parent process (host) finishes, the current process must end.
Environment.Exit(0);
}