当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  Net Remoting基础篇3

 Net Remoting基础篇3

点击次数:22 次 发布日期:2008-11-26 10:43:54 作者:源代码网
源代码网推荐      (2) 客户端激活模式
源代码网推荐  
源代码网推荐  如前所述,WellKnown模式在客户端创建对象时,只能调用默认的构造函数,上面的代码就说明了这一点,因为GetObject()方法不能传递构造函数的参数。而客户端激活模式则可以通过自定义的构造函数来创建远程对象。
源代码网推荐  
源代码网推荐  客户端激活模式有两种方法:
源代码网推荐  1) 调用RemotingConfiguration的静态方法RegisterActivatedClientType()。这个方法返回值为Void,它只是将远程对象注册在客户端而已。具体的实例化还需要调用对象类的构造函数。
源代码网推荐   RemotingConfiguration.RegisterActivatedClientType(
源代码网推荐   typeof(ServerRemoteObject.ServerObject),
源代码网推荐   "tcp://localhost:8080/ServiceMessage");
源代码网推荐   ServerRemoteObject.ServerObject serverObj = new ServerRemoteObject.ServerObject();
源代码网推荐  
源代码网推荐  2) 调用进程Activator的CreateInstance()方法。这个方法将创建方法参数指定类型的类对象。它与前面的GetObject()不同的是,它要在客户端调用构造函数,而GetObject()只是获得对象,而创建实例是在服务器端完成的。CreateInstance()方法有很多个重载,我着重说一下其中常用的两个。
源代码网推荐  a、 public static object CreateInstance(Type type, object[] args, object[] activationAttributes);
源代码网推荐  
源代码网推荐  参数说明:
源代码网推荐  type:要创建的对象的类型。
源代码网推荐  args :与要调用构造函数的参数数量、顺序和类型匹配的参数数组。如果 args 为空数组或空引用(Visual Basic 中为 Nothing),则调用不带任何参数的构造函数(默认构造函数)。
源代码网推荐  activationAttributes :包含一个或多个可以参与激活的属性的数组。
源代码网推荐  
源代码网推荐  这里的参数args是一个object[]数组类型。它可以传递要创建对象的构造函数中的参数。从这里其实可以得到一个结论:WellKnown激活模式所传递的远程对象类,只能使用默认的构造函数;而Activated模式则可以用户自定义构造函数。activationAttributes参数在这个方法中通常用来传递服务器的url。
源代码网推荐  假设我们的远程对象类ServerObject有个构造函数:
源代码网推荐   ServerObject(string pName,string pSex,int pAge)
源代码网推荐   {
源代码网推荐   name = pName;
源代码网推荐   sex = pSex;
源代码网推荐   age = pAge;
源代码网推荐   }
源代码网推荐  
源代码网推荐  那么实现的代码是:
源代码网推荐   object[] attrs = {new UrlAttribute("tcp://localhost:8080/ServiceMessage")};
源代码网推荐   object[] objs = new object[3];
源代码网推荐   objs[0] = "wayfarer";
源代码网推荐   objs[1] = "male";
源代码网推荐   objs[2] = 28;
源代码网推荐   ServerRemoteObject.ServerObject = Activator.CreateInstance(
源代码网推荐   typeof(ServerRemoteObject.ServerObject),objs,attrs);
源代码网推荐  可以看到,objs[]数组传递的就是构造函数的参数。
源代码网推荐  
源代码网推荐  b、public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttribute);
源代码网推荐  
源代码网推荐  参数说明:
源代码网推荐  assemblyName :将在其中查找名为 typeName 的类型的程序集的名称。如果 assemblyName 为空引用(Visual Basic 中为 Nothing),则搜索正在执行的程序集。
源代码网推荐  typeName:首选类型的名称。
源代码网推荐  activationAttributes :包含一个或多个可以参与激活的属性的数组。
源代码网推荐  
源代码网推荐  参数说明一目了然。注意这个方法返回值为ObjectHandle类型,因此代码与前不同:
源代码网推荐   object[] attrs = {new UrlAttribute("tcp://localhost:8080/EchoMessage")};
源代码网推荐   ObjectHandle handle = Activator.CreateInstance("ServerRemoteObject",
源代码网推荐   "ServerRemoteObject.ServerObject",attrs);
源代码网推荐   ServerRemoteObject.ServerObject obj = (ServerRemoteObject.ServerObject)handle.Unwrap();
源代码网推荐  
源代码网推荐  这个方法实际上是调用的默认构造函数。ObjectHandle.Unwrap()方法是返回被包装的对象。
源代码网推荐  
源代码网推荐  说明:要使用UrlAttribute,还需要在命名空间中添加:using System.Runtime.Remoting.Activation;
源代码网推荐  
源代码网推荐  五、Remoting基础的补充
源代码网推荐  
源代码网推荐  通过上面的描述,基本上已经完成了一个最简单的Remoting程序。这是一个标准的创建Remoting程序的方法,但在实际开发过程中,我们遇到的情况也许千奇百怪,如果只掌握一种所谓的“标准”,就妄想可以“一招鲜、吃遍天”,是不可能的。
源代码网推荐  
源代码网推荐  1、注册多个通道
源代码网推荐  
源代码网推荐  在Remoting中,允许同时创建多个通道,即根据不同的端口创建不同的通道。但是,Remoting要求通道的名字必须不同,因为它要用来作为通道的唯一标识符。虽然IChannel有ChannelName属性,但这个属性是只读的。因此前面所述的创建通道的方法无法实现同时注册多个通道的要求。
源代码网推荐  
源代码网推荐  这个时候,我们必须用到System.Collection中的IDictionary接口:
源代码网推荐  
源代码网推荐  注册Tcp通道:
源代码网推荐  IDictionary tcpProp = new Hashtable();
源代码网推荐  tcpProp["name"] = "tcp9090";
源代码网推荐  tcpProp["port"] = 9090;
源代码网推荐  IChannel channel = new TcpChannel(tcpProp,
源代码网推荐   new BinaryClientFormatterSinkProvider(),
源代码网推荐   new BinaryServerFormatterSinkProvider());
源代码网推荐  ChannelServices.RegisterChannel(channel);
源代码网推荐  
源代码网推荐  注册Http通道:
源代码网推荐  IDictionary httpProp = new Hashtable();
源代码网推荐  httpProp["name"] = "http8080";
源代码网推荐  httpProp["port"] = 8080;
源代码网推荐  IChannel channel = new HttpChannel(httpProp,
源代码网推荐   new SoapClientFormatterSinkProvider(),
源代码网推荐   new SoapServerFormatterSinkProvider());
源代码网推荐  ChannelServices.RegisterChannel(channel);
源代码网推荐  
源代码网推荐  在name属性中,定义不同的通道名称就可以了。
源代码网推荐  
源代码网推荐  2、远程对象元数据相关性
源代码网推荐  
源代码网推荐  由于服务器端和客户端都要用到远程对象,通常的方式是生成两份完全相同的对象Dll,分别添加引用。不过为了代码的安全性,且降低客户端对远程对象元数据的相关性,我们有必要对这种方式进行改动。即在服务器端实现远程对象,而在客户端则删除这些实现的元数据。
源代码网推荐  
源代码网推荐  由于激活模式的不同,在客户端创建对象的方法也不同,所以要分离元数据的相关性,也应分为两种情况。
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华