Although you can make a call to a delegate that will be handled by a separate AppDomain, I personally always used the CreateInstanceAndUnwrap method, which creates an object in a foreign application domain and returns a proxy to it.
To do this, your object must inherit from MarshalByRefObject.
Here is an example:
public interface IRuntime { bool Run(RuntimesetupInfo setupInfo); }
In the above example, it is encoded to execute the Run method, which is passed in some installation information, and the completion of the Run method is determined to indicate that all the code in the child domain of the AppDomain is complete, so we finally have a block that enables the unloading of the AppDomain.
Often you can be careful what types you place in assemblies - you can use the interface and put it in a separate assembly, which both the calling program (our code that installs the application and calls it) and the developer (Runtime class) depend on. This IIRC allows the parent AppDomain to load only the assembly containing the interface, while the child domain appdomain loads both the assembly containing Runtime and its dependency (IRuntime assembly). Any custom types that are used by the IRuntime interface (for example, our RuntimeSetupInfo class) should usually also be placed in the same assembly as IRuntime. Also, be careful how you define these custom types β if they are data objects (probably RuntimeSetupInfo), you should probably mark them with the [serializable] attribute β so that a copy of the object is passed (serialized from the parent appdomain for a child). You want to avoid redirecting calls from one application to another, as it is rather slow. Passing a DTO by value (serialization) means that accessing the values ββin the DTO does not require an internetwork call (since the child appdomain has its own copy of the original). Of course, this also means that cost changes are not reflected in the original source DTO of the parent application.
As encoded in the example, the parent appdomain will actually load both the IRuntime and Runtime assemblies, but this is because when I call CreateInstanceAndUnwrap I use typeof (Runtime) to get the assembly name and full type name. Instead, you can hard-code or Extract these lines from the file, which separates the dependency.
There is also a method in AppDomain called "DoCallBack" that looks like it allows you to call a delegate in a foreign AppDomain. However, the type of delegate that it accepts is of type "CrossAppDomainDelegate". The definition of which:
public delegate void CrossAppDomainDelegate()
Thus, it will not allow you to transfer any data to it. And, since I never used it, I canβt tell you if there are any special problems.
In addition, I would recommend exploring the LoaderOptimization property. What you set for this can significantly affect performance, since some settings of this property force the new appdomain to load separate copies of all assemblies (and their JITs, etc.), even if (IIRC) the assembly is in the GAC (i.e. E. This includes CLR assemblies). This can give you terrible performance if you use a large number of assemblies from your child application. For example, I used WPF from child domains, which caused big launch delays for my application, until I set a more suitable download policy.