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

 Net Remoting基础篇2

点击次数:21 次 发布日期:2008-11-26 10:43:56 作者:源代码网
源代码网推荐      三、服务器端
源代码网推荐  
源代码网推荐  根据第一部分所述,根据激活模式的不同,通道类型的不同服务器端的实现方式也有所不同。大体上说,服务器端应分为三步:
源代码网推荐  
源代码网推荐  1、注册通道
源代码网推荐  
源代码网推荐  要跨越应用程序域进行通信,必须实现通道。如前所述,Remoting提供了IChannel接口,分别包含TcpChannel和HttpChannel两种类型的通道。这两种类型除了性能和序列化数据的格式不同外,实现的方式完全一致,因此下面我们就以TcpChannel为例。
源代码网推荐  
源代码网推荐  注册TcpChannel,首先要在项目中添加引用“System.Runtime.Remoting”,然后using名字空间:System.Runtime.Remoting.Channel.Tcp。代码如下:
源代码网推荐   TcpChannel channel = new TcpChannel(8080);
源代码网推荐   ChannelServices.ReGISterChannel(channel);
源代码网推荐  
源代码网推荐  在实例化通道对象时,将端口号作为参数传递。然后再调用静态方法ReGISterChannel()来注册该通道对象即可。
源代码网推荐  
源代码网推荐  2、注册远程对象
源代码网推荐  
源代码网推荐  注册了通道后,要能激活远程对象,必须在通道中注册该对象。根据激活模式的不同,注册对象的方法也不同。
源代码网推荐  
源代码网推荐  (1) SingleTon模式
源代码网推荐  
源代码网推荐  对于WellKnown对象,可以通过静态方法RemotingConfiguration.RegisterWellKnownServiceType()来实现:RemotingConfiguration.RegisterWellKnownServiceType(
源代码网推荐   typeof(ServerRemoteObject.ServerObject),
源代码网推荐   "ServiceMessage",WellKnownObjectMode.SingleTon);
源代码网推荐  
源代码网推荐  (2)SingleCall模式
源代码网推荐  
源代码网推荐  注册对象的方法基本上和SingleTon模式相同,只需要将枚举参数WellKnownObjectMode改为SingleCall就可以了。RemotingConfiguration.RegisterWellKnownServiceType(
源代码网推荐   typeof(ServerRemoteObject.ServerObject),
源代码网推荐   "ServiceMessage",WellKnownObjectMode.SingleCall);
源代码网推荐  
源代码网推荐  (3)客户端激活模式
源代码网推荐  
源代码网推荐  对于客户端激活模式,使用的方法又有不同,但区别不大,看了代码就一目了然。
源代码网推荐  RemotingConfiguration.ApplicationName = "ServiceMessage";
源代码网推荐  RemotingConfiguration.RegisterActivatedServiceType(
源代码网推荐   typeof(ServerRemoteObject.ServerObject));
源代码网推荐  
源代码网推荐  为什么要在注册对象方法前设置ApplicationName属性呢?其实这个属性就是该对象的URI。对于WellKnown模式,URI是放在RegisterWellKnownServiceType()方法的参数中,当然也可以拿出来专门对ApplicationName属性赋值。而RegisterActivatedServiceType()方法的重载中,没有ApplicationName的参数,所以必须分开。
源代码网推荐  
源代码网推荐  3、注销通道
源代码网推荐  
源代码网推荐  如果要关闭Remoting的服务,则需要注销通道,也可以关闭对通道的监听。在Remoting中当我们注册通道的时候,就自动开启了通道的监听。而如果关闭了对通道的监听,则该通道就无法接受客户端的请求,但通道仍然存在,如果你想再一次注册该通道,会抛出异常。
源代码网推荐  
源代码网推荐   //获得当前已注册的通道;
源代码网推荐   IChannel[] channels = ChannelServices.RegisteredChannels;
源代码网推荐  
源代码网推荐   //关闭指定名为MyTcp的通道;
源代码网推荐   foreach (IChannel eachChannel in channels)
源代码网推荐   {
源代码网推荐   if (eachChannel.ChannelName == "MyTcp")
源代码网推荐   {
源代码网推荐   TcpChannel tcpChannel = (TcpChannel)eachChannel;
源代码网推荐  
源代码网推荐   //关闭监听;
源代码网推荐   tcpChannel.StopListening(null);
源代码网推荐  
源代码网推荐   //注销通道;
源代码网推荐   ChannelServices.UnregisterChannel(tcpChannel);
源代码网推荐   }
源代码网推荐   }
源代码网推荐  代码中,RegisterdChannel属性获得的是当前已注册的通道。在Remoting中,是允许同时注册多个通道的,这一点会在后面说明。
源代码网推荐  
源代码网推荐  四、客户端
源代码网推荐  
源代码网推荐  客户端主要做两件事,一是注册通道。这一点从图一就可以看出,Remoting中服务器端和客户端都必须通过通道来传递消息,以获得远程对象。第二步则是获得该远程对象。
源代码网推荐  
源代码网推荐  1、注册通道:
源代码网推荐  TcpChannel channel = new TcpChannel();
源代码网推荐  ChannelServices.RegisterChannel(channel);
源代码网推荐  
源代码网推荐  注意在客户端实例化通道时,是调用的默认构造函数,即没有传递端口号。事实上,这个端口号是缺一不可的,只不过它的指定被放在后面作为了Uri的一部分。
源代码网推荐  
源代码网推荐  2、获得远程对象。
源代码网推荐  
源代码网推荐  与服务器端相同,不同的激活模式决定了客户端的实现方式也将不同。不过这个区别仅仅是WellKnown激活模式和客户端激活模式之间的区别,而对于SingleTon和SingleCall模式,客户端的实现完全相同。
源代码网推荐  
源代码网推荐  (1) WellKnown激活模式
源代码网推荐  
源代码网推荐  要获得服务器端的知名远程对象,可通过Activator进程的GetObject()方法来获得:
源代码网推荐  ServerRemoteObject.ServerObject serverObj = (ServerRemoteObject.ServerObject)Activator.GetObject(
源代码网推荐   typeof(ServerRemoteObject.ServerObject), "tcp://localhost:8080/ServiceMessage");
源代码网推荐  
源代码网推荐  首先以WellKnown模式激活,客户端获得对象的方法是使用GetObject()。其中参数第一个是远程对象的类型。第二个参数就是服务器端的uri。如果是http通道,自然是用http://localhost:8080/ServiceMessage了。因为我是用本地机,所以这里是localhost,你可以用具体的服务器IP地址来代替它。端口必须和服务器端的端口一致。后面则是服务器定义的远程对象服务名,即ApplicationName属性的内容。
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


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