.net remoting accross App Domain

下面这个例子介绍了如何利用.net remoting 来实现App Domain间的访问,而且通过创建一个新的App Domain来加载一些untrusted的dlls,保证了程序自身的App Domain的边界安全。
下载
下面是程序的main函数:
namespace Microsoft.Samples.Application
{
    
static class Program
    {
        
static void Main()
        {
            
//Create new appDomain
            AppDomain domain = AppDomain.CreateDomain("NewAppDomain");

            
// Create remote object in new appDomain via shared interface
            // to avoid loading the implementation library into this appDomain
            IHelloWorld proxy = 
                (IHelloWorld)domain.CreateInstanceAndUnwrap(
                    
"ImplementationLibrary"
                    
"Microsoft.Samples.ImplementationLibrary.HelloWorld");

            
// Output results of the call
            Console.WriteLine("\nReturn:\n\t{0}", proxy.Echo("Hello"));
            Console.WriteLine();

            Console.WriteLine(
"Non-GAC assemblies loaded in {0} appDomain:", AppDomain.CurrentDomain.FriendlyName);
            
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                
if (!assembly.GlobalAssemblyCache)
                {
                    Console.WriteLine(
"\t" + assembly.GetName().Name);
                }
            }
            Console.WriteLine(
"\nImplementationLibrary should not be loaded.");

            Console.ReadLine();
        }
    }
}

下面是被load进新创建的Domaindll

namespace Microsoft.Samples.ImplementationLibrary
{
    
internal class HelloWorld : MarshalByRefObject, IHelloWorld
    {

        
#region IHelloWorld Members

        
string IHelloWorld.Echo(string input)
        {
            ConsoleColor originalColor = Console.BackgroundColor;
            Console.BackgroundColor = ConsoleColor.Blue;

            
string currentAppDomainName = AppDomain.CurrentDomain.FriendlyName;
            Console.WriteLine(
"AppDomain: {0}", currentAppDomainName);
            Console.WriteLine(
"Echo Input: {0}", input);
            Console.WriteLine();
            Console.WriteLine(
"Non-GAC assemblies loaded in {0} appDomain:", AppDomain.CurrentDomain.FriendlyName);
            
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                
if (!assembly.GlobalAssemblyCache)
                {
                    Console.WriteLine(
"\t" + assembly.GetName().Name);
                }
            }


            Console.BackgroundColor = originalColor;

            
            
return input + " from AppDomain: " + currentAppDomainName;

        }

        
#endregion

    }
}
程序的输出结果是:
AppDomain: NewAppDomain
Echo Input: Hello

Non-GAC assemblies loaded in NewAppDomain appDomain:
 ImplementationLibrary
 SharedInterface

Return:
 Hello from AppDomain: NewAppDomain

Non-GAC assemblies loaded in Application.vshost.exe appDomain:
 vshost
 Application
 SharedInterface

ImplementationLibrary should not be loaded.

另外解释一下Unwarp()函数的作用,例如ObjectHandleAssembly有一个MyType的类:
// Creates an instance of MyType defined in the assembly called ObjectHandleAssembly.
ObjectHandle obj = domain.CreateInstance( "ObjectHandleAssembly", "MyType");

// Unwrapps the proxy to the MyType object created in the other AppDomain.
MyType testObj = (MyType)obj. Unwrap();

你可能感兴趣的:(domain)