博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net 之不同AppDomin之间通讯
阅读量:7067 次
发布时间:2019-06-28

本文共 5629 字,大约阅读时间需要 18 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Reflection;using System.Runtime.Remoting;namespace Marshal{    class Program    {        static void Main(string[] args)        {            // 获取一个指向应用程序域的引用            AppDomain adCallingThreadDomain = Thread.GetDomain();            // 每一个应用程序域都会被分配一个名字帮助调试 ,以下代码获取引用程序域的名字            String callingDomainName = adCallingThreadDomain.FriendlyName;            // 获取应用程序域的完整名            String exeAssembly = Assembly.GetEntryAssembly().FullName;            AppDomain ad2 = null;            // ************************************************************************************************************            // 使用Marshal-by-Reference的方式跨域通信            Console.WriteLine("{0}Demo #1", Environment.NewLine);            // 建立一个域,安全和配置均使用当前域的设置            ad2 = AppDomain.CreateDomain("AD #2", null, null);            MarshalByRefType mbrt = null;            // 加载Assembly到新的域,new一个对象并且返回到当前域 (实际上返回的是一个引用代理)             mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "Marshal.MarshalByRefType");            Console.WriteLine("Type={0}", mbrt.GetType()); // CLR 对GetType做了一些手脚,返回的是被代理数据的真实类型            // 以下代码证明我们获取的是一个代理对象            Console.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));            // 我们调用了代理类的SomeMethod() , 代理类跨域访问真正的对象            mbrt.SomeMethod();            // 卸载新建的那个应用程序域            AppDomain.Unload(ad2);            // mbrt refers to a valid proxy object; the proxy object refers to an invalid AppDomain            try            {                // 再次调用代理类的SomeMethod() , 由于域已经被卸载 抛出一个异常                mbrt.SomeMethod();                Console.WriteLine("Successful call.");            }            catch (AppDomainUnloadedException)            {                Console.WriteLine("Failed call.");            }            // ************************************************************************************************************            // 使用Marshal-by-Value 的方式跨域通信            Console.WriteLine("{0}Demo #2", Environment.NewLine);            // 新建域            ad2 = AppDomain.CreateDomain("AD #2", null, null);            // 加载程序集并创建代理类            mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "Marshal.MarshalByRefType");            // 该方法返回了一个值的拷贝 marshaled by value (not be reference).            MarshalByValType mbvt = mbrt.MethodWithReturn();            // 证明返回值不是一个代理类            Console.WriteLine("Is proxy={0}", RemotingServices.IsTransparentProxy(mbvt));            // 查看返回值是谁创建的            Console.WriteLine("Returned object created " + mbvt.ToString());            // 卸载应用程序域            AppDomain.Unload(ad2);            // 由于是值传递,那么卸载域对函数没有影响 // marshaled by value             try            {                //不会有异常抛出                Console.WriteLine("Returned object created " + mbvt.ToString());                Console.WriteLine("Successful call.");            }            catch (AppDomainUnloadedException)            {                Console.WriteLine("Failed call.");            }            // ************************************************************************************************************            // non-marshalable type 跨域通信            Console.WriteLine("{0}Demo #3", Environment.NewLine);            ad2 = AppDomain.CreateDomain("AD #2", null, null);            mbrt = (MarshalByRefType)ad2.CreateInstanceAndUnwrap(exeAssembly, "TestConsole.MarshalByRefType");            // 没有标记为 Serializable 的类型对象 不能通过marshaled by value 跨域通信            NonMarshalableType nmt = mbrt.MethodArgAndReturn(callingDomainName);        }    }    //即使没有标记为Serializable 也可以通过 marshaled-by-reference 的方式跨域通信    public sealed class MarshalByRefType : MarshalByRefObject    {        public MarshalByRefType()        {            Console.WriteLine("{0} ctor running in {1}",            this.GetType().ToString(), Thread.GetDomain().FriendlyName);        }        public void SomeMethod()        {            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);        }        public MarshalByValType MethodWithReturn()        {            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);            MarshalByValType t = new MarshalByValType();            return t;        }        public NonMarshalableType MethodArgAndReturn(String callingDomainName)        {            // NOTE: callingDomainName is [Serializable]            Console.WriteLine("Calling from ‘{0}’ to ‘{1}’.",            callingDomainName, Thread.GetDomain().FriendlyName);            NonMarshalableType t = new NonMarshalableType();            return t;        }    }    //只有标记为 Serializable 的类型 才能用marshaled by value 的方式跨域通信    [Serializable]    public sealed class MarshalByValType : Object      {        private DateTime m_creationTime = DateTime.Now; // NOTE: DateTime is [Serializable]        public MarshalByValType()        {            Console.WriteLine("{0} ctor running in {1}, Created on {2:D}",            this.GetType().ToString(),            Thread.GetDomain().FriendlyName,            m_creationTime);        }        public override String ToString()        {
 		 return m_creationTime.ToLongDateString();        }    }    // 没有标记为 Serializable 的类型 不能用marshaled by value 的方式跨域通信    // [Serializable]    public sealed class NonMarshalableType : Object    {        public NonMarshalableType()        {            Console.WriteLine("Executing in " + Thread.GetDomain().FriendlyName);        }    }}

显示结果为:显示结果

 

 

转载于:https://www.cnblogs.com/voidobject/archive/2012/11/06/3975489.html

你可能感兴趣的文章
写sql语句时将时间格式“20110725”转化为格式2012年07月25日
查看>>
[Hadoop in China 2011] 蒋建平:探秘基于Hadoop的华为共有云
查看>>
heartbeat高可用+lvsDR
查看>>
方丈被害子女有没有权利继承遗产?
查看>>
java入门第一季5、6
查看>>
[转载] 闻一多——七子之歌
查看>>
针对tomcat日志乱码问题
查看>>
免费的协作和协同办公软件平台onlyoffice轻松部署
查看>>
WiFi覆盖下的生活 享受便利的同时 别忘记了安全
查看>>
关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )...
查看>>
Linux软件包的管理--YUM
查看>>
Axis2发布webservice(1)--0配置发布
查看>>
Java Web笔记 – Servlet中的Filter过滤器的介绍和使用 编写过滤器
查看>>
我奋斗了18年,不是为了和你一起喝咖啡
查看>>
gearman简单介绍
查看>>
《Typecript 入门教程》 3、接口
查看>>
jsp的几种跳转比较
查看>>
用oracle查询当前数据库中的所有表
查看>>
决心书
查看>>
git 从版本控制中删除文件及.gitignore的用法
查看>>