This project is read-only.

The usage of ProcessDomain is very similar to that of AppDomain with only a few minor exceptions: ProcessDomain also implements the IDisposable pattern for convenience, and, not all versions of CreateInstanceX() functions are implemented yet. To create a ProcessDomain, simply use the CreateDomain() function as you would for AppDomains:

   var domain = ProcessDomain.CreateDomain("MyDomain");

The 'friendlyName' of a ProcessDomain affects what the name of the remote process will be. In this case, we chose 'MyDomain' as the friendly name, as a result, in Task Manager, you would see a process named MyDomain.exe, which would host an AppDomain that is configured the same way as the current AppDomain. This means the remote process uses the same Configuration File, same Base Directory, and so forth. You may specify your own custom settings if you wish by passing in a ProcessDomainSetup instance as well. ProcessDomainSetup wraps some basic options that will be used for starting the remote process, options related to the creating the process domain, and options for the remote AppDomain's setup information. For example, if you wanted to prevent the remote process from restarting after a crash, you would configure it in the following way:

   var setup = new ProcessDomainSetup
      RestartOnProcessExit = false

   var domain = ProcessDomain.CreateDomain("MyDomain", setup);

Once the process domain is created, it is either destroyed by using the IDisposable pattern or by calling Unload():

   using(var domain = ProcessDomain.CreateDomain("MyDomain"))
      // do stuff in our domain

   var domain = ProcessDomain.CreateDomain("MyDomain");

You can also subscribe to two events on ProcessDomain that allow you to be notified when the remote process exits or restarts. These events are called Detached and Attached, respectively. The Detached event will fire when the remote process is explicitly unloaded, but, the Attached event will not fire when the process domain is first created because the process is already created before the events can be subscribed to. Now that you have a process domain, you can create and unwrap instances of serializable objects in this new process domain. For now, only the three basic CreateInstanceAndUnwrap implementations are supported, but should suit most needs. The AppDomain in the remote process will have all the same assembly references as the AppDomain in which the process domain was created from. The only exception to this is dynamic assemblies. To create a proxy for an object in the remote process, you can simply do something like this:

   using(var domain = ProcessDomain.CreateDomain("MyDomain"))
      var remoteObject = (RemoteObject)domain.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);
      Console.WriteLine("Remote Property Value: {0}", remoteObject.SomePropertyValueOfRemoteObject);

For more examples and usages, see the AppSecInc.ProcessDomain.Sample project.

For projects being built with .NET Framework 4.0 and higher

ProcessDomain compiles an assembly at runtime. In order for the compiled assembly to be able to run properly, you may need to set the CompilerVersion property in ProcessDomainSetup. For example, if your project where you use ProcessDomain uses .NET Framework 4.0 or 4.5, you will need to set this property to "v4.0". Failing to do so will lead to a BadImageFormatException when trying to load ProcessDomain.

Last edited Mar 19, 2014 at 2:39 PM by eternal0, version 2


No comments yet.