How to Execute an Application in a Remote Application Domain

This article explains executing an application in a remote application domain

Introduction

Application domain is a construct in the CLR that is the unit of isolation for an application. Application domains provide a secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. You can run several application domains in a single process with the same level of isolation that would exist in separate processes, but without incurring the additional overhead of making cross-process calls or switching between processes. The ability to run multiple applications within a single process dramatically increases server scalability.

 

Make a file CallingAssembly.cs containing following code

 

using System;

using System.Reflection;

using System.Runtime.Remoting;
public class CallingAssembly

{

    public static void Main(String[] argv)

    {

        AppDomainSetup objADS = new AppDomainSetup();

        objADS.ApplicationBase = "file:///" + Environment.CurrentDirectory;

        AppDomain objAD = AppDomain.CreateDomain("RemoteDomainExample", null, objADS);

        objAD.ExecuteAssembly("CalledAssembly.exe");

        AppDomain.Unload(objAD);

    }

}

 

Two Application domains interact with each other through Remoting. AppDomain class is drived from MarshalByRefObject, which Enables access to objects across application domain boundaries in applications that support remoting. CreateDomain creates a new application domain with the given name using the assembly binding information  and supplied evidence. ExecuteAssembly methos of AppDomain, Executes the assembly contained in the specified file in that domain.

 

Make a file CalledAssembly.cs containing following code

 

using System;

using System.Threading;

public class CalledAssembly

{

    public static void Main(String[] argv)

    {

        Console.WriteLine("Called Assembly Application Domain: " + Thread.GetDomain().FriendlyName);

        Console.WriteLine("Assembly Called");

    }

}

 

Put both files in same folder and compile as shown in following image

RemoteApp1.gif 


Now CalledAssembly is running in Remote Domain instead of default domain.