学习remoting的一部份总结
服务端:
一.可远程化对象 (继承MarshalByRefObject),注意:不能远程处理私有方法;
二.数组应用程序(远程对象服务器),监听客户端请求:
1)注册通道/信道
TcpChannel chan=new TcpChannel(8085);
HttpChannel chan=new HttpChannel(8085);
2)注册激活远程对象 (两种激活方式:服务端(singlecall,singleton),和客户端)
RemotingConfiguration.RegisterWellKnownServiceType(Type type,string url,WellKnownObjectMode mode)
-------------------------------------------------------------------------------------------
例: //注册通道,确定对象服务器在哪个通道上的哪个端口进行监听
TcpChannel chan=new TcpChannel(863);
ChannelServices.RegisterChannel(chan);
//激活方式
WellKnownServiceTypeEntry a=new WellKnownServiceTypeEntry(
typeof(ServerClass.UserData),
"testserver",//客户端调用远程对象时将使用此uri
WellKnownObjectMode.Singleton//激活模式
);
RemotingConfiguration.ApplicationName="servername";//应用程序名
RemotingConfiguration.RegisterWellKnownServiceType(a);
Console.WriteLine("server started ! press<enter>exit");
Console.ReadLine();
客户端使用uri:ProtocolScheme://ComputerName:Port/PossibleApplicationName/ObjectUri
------------------------------------------------------------------------------------
客户端激活方式:
------------------------------------------------------------
TcpChannel chan=new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.ApplicationName="hello";//相当于该对象的uri
RemotingConfiguration.RegisterActivatedServiceType(Type.GetType("ffff"));
Console.WriteLine("server started ! press<enter>exit");
Console.ReadLine();
或
ActivatedServiceTypeEntry ASTE = new ActivatedServiceTypeEntry(typeof(WiseOwl.Calculator)); RemotingConfiguration.RegisterActivatedServiceType(ASTE);
......
客户端使用uri:ProtocolScheme://ComputerName:Port/PossibleApplicationName
---------------------------------------------------------
3)等待客户链接
客户端:
一.注册信道
TcpChannel chan=new TcpChannel()或HttpChannel
不用指明端口号,因为只用着客户端信道,不在任何端口上侦听.
在客户端注册信息不是必需的,如果客户端示注册信道,则远程处理系统将使用在machine.config中指定默认信道之一自动选择或创建一个信道以发出请求.
二.生成远程服务对象的代理
Activator.GetObject(Type type,string url)
并不会在服务端创建远程对象的实例,它仅在客户端根据"远程对象的类型信息"创建对应于"远程对象的本地代理".
另可以用new 生成远程对象的本地代理,必须首先用RegisterWellKnownClientType()在客户端注册该已知对象类型,调用向远程处理基础结构提供远程对象的位置.
如:RemotingConfiguration.RegisterWellKnownClientType(typeof(hello),url);//客户端激活的用RemotingConfiguration.RegisterActivatedClientType(typeof(fff),uri);
hello obj =new hello();
三.通过代理调用远程对象的方法
通过代理第一次调用远程对象时,"远程对象服务器"将调用默认的构造函数生成远程对象实例.
注:通过客户端激活的对象则用以下方法取得远程对象代理
Server obj=(Server)Activator.CreateInstance(.......)[.Unwrap()];
如:object[] att=new object[]{new UrlAttribute(url)};
Object a=Activator.CreateInstance(typeof(ServerClass.UserData),null,att);
本文深入讲解了Remoting技术,包括服务端与客户端的配置过程,远程对象的激活方式,以及如何通过代理调用远程对象的方法。详细介绍了TcpChannel和HttpChannel的使用,以及WellKnownServiceType和ActivatedServiceTypeEntry的注册方法。
49

被折叠的 条评论
为什么被折叠?



