Exemplo n.º 1
0
        public MarshalByValType MethodWithReturn()
        {
            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);
            MarshalByValType t = new MarshalByValType();

            return(t);
        }
Exemplo n.º 2
0
        private static void Marshling()
        {
            //获取AppDomain引用
            AppDomain adCallingThreadDomain = Thread.GetDomain();

            string callingDomainName = adCallingThreadDomain.FriendlyName;

            Console.WriteLine("Default AppDomain's friendly name={0}", callingDomainName);

            //获取并显示我们的AppDomain中包含的“Main”方法的程序集
            string exeAssembly = Assembly.GetEntryAssembly().FullName;

            Console.WriteLine("Main assembly={0}", exeAssembly);

            //***DEMO 1:使用按引用封送进行跨AppDomain通信***
            Console.WriteLine("{0} Demo #1", Environment.NewLine);

            //定义局部变量来引用一个AppDomain
            AppDomain ad2 = AppDomain.CreateDomain("AD #2", null, null);

            //将我们的程序集加载到新AppDomain中,构造一个对象,把它封送回我们的AppDomain(实际得到的是一个代理的引用)
            MarshalByRefType mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "ConsoleAppDomains.MarshalByRefType");

            Console.WriteLine("Type={0}", mbrt.GetType());

            //证明得到的是一个代理对象的引用
            Console.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));

            //代理类型上调用一个方法,代理使线程切换到拥有对象的那个AppDomain,并在真实的对象上调用这个方法。
            mbrt.SomeMethod();

            //卸载新的AppDomain
            AppDomain.Unload(ad2);

            try
            {
                //在代理上调用一个方法。AppDomain无效,造成抛出异常
                mbrt.SomeMethod();
                Console.WriteLine("Successful call.");
            }
            catch (AppDomainUnloadedException)
            {
                Console.WriteLine("Failed call.");
            }

            //***DEMO 2:使用Marshal-by-Value进行跨AppDomain通信***
            Console.WriteLine("{0} Demo #2", Environment.NewLine);

            //新建一个AppDomain
            ad2  = AppDomain.CreateDomain("AD #2", null, null);
            mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "ConsoleAppDomains.MarshalByRefType");
            MarshalByValType mbvt = mbrt.MethodWithReturn();

            Console.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(mbvt));

            Console.WriteLine("Returned object created " + mbvt.ToString());

            AppDomain.Unload(ad2);

            try
            {
                Console.WriteLine("Returned object created " + mbvt.ToString());
                Console.WriteLine("Success call.");
            }
            catch (AppDomainUnloadedException)
            {
                Console.WriteLine("Failed call.");
            }


            //***DEMO 3:使用不可封送的类型进行跨AppDomain通信**
            Console.WriteLine("{0} Demo #3", Environment.NewLine);

            //新建一个AppDomain
            ad2  = AppDomain.CreateDomain("AD #2", null, null);
            mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "ConsoleAppDomains.MarshalByRefType");

            NoMarshalableType nmt = mbrt.MethodArgAndReturn(callingDomainName);
        }