Remoting学习笔记

来源:互联网 发布:霸主软件骗局 编辑:程序博客网 时间:2024/05/15 12:10

一:--------------------Dll Remotable类

 

public class RemotableClass:MarshalByRefObject

{

...

}

 

 

二:----------------------remoting服务端注册remotable类

 

服务器通过两种静态方法来注册:

 

RegisterActivatedServiceType  RegisterWellKnownServiceType

 

eg:注册Remotableclasss

 

RemotingConfigration.RegisterWellknowServiceType(

Typeof(RemotableClas), //指能远程化的类

"RemoutObject",   //Remotable类的URI

WellkownOjectMode.SingleCall //激活模式 为每个客户端创建一个新的实例 WellkownObjectMode.SingleTon 为所有客户端创建一个RemotableClass实例来处理所有客户端的调

 

);

 

 

三:---------------------------服务端创建,注册一个通道

System.Runtime.Remoting.Channels.Tcp.TcpServerChannel:可以接受远程客户端的TCP连接

System.Runtime.Remoting.Channels.Http.HttpServerChannel:接受Http连接

 

 

EG:使用.NET框架

创建一个 1234 端口监听的 TcpServerChannel通道,并用.NET框架注册

TcpServerChannel channel=new TcpServerChannel(1234);

ChannelService.RegisterChannel(channel);

 

HttpServerChannel channel=new HttpServerChannel(1234);

ChannelService.RegisterChannel(channel);

 

 

四:-----------------------------客户端创建远程类的一个实例,也必须做一些注册

1:必须创建并注册一个客户端通道,

.net客户端通道类型:

TcpClientChannel HttpClientChannel

 

2:new 远程对象前提 将远程对象注册到本地应用程序域

RemotingConfiguration.RegisterWellKownClientType 在客户端注册一个类

RemotingConfiguration.RegisterWellKownServiceType 在服务器上注册一个类

 

EG:

 

TcpClientChannel channel=new TcpClientChannel();

ChannelServices.RegisterChannel(channel);

RemotingConfiguration.RegisterWellKownClientType(

Typeof(RemotableClass),

"tcp://localhost:1234/RemoteObject"

);

 

五:-------------------------------客户端 new 一个代理

 

//在客户端应用程序域中产生一个代理,返回RemotableClas的一个引用

RemotableClass rc=new RemotableClass();

 

EG:ClockServer

 

配置文件可以读取AppConfig

RemotingConfiguration.Configure("TimeServer.exe.config");

TimeServer.exe.config

<configuration>

<system.runtime.remoting>

<applicatoin>

<service>

<wellkown mode="SingleCall" type="Clock,ClockServer" object="Clock">

</service>

<channels>

<channel ref="tcp server" port="1234">

</channels>

</application>

</system.runtime.remoting>

</configuration>

 

激活方式的差别

服务器端激活对象

RegisterWellKownServiceType RegisterWellKownClientType

客户端激活对象

RegisterActivateServiceType RegisterActivateClientType

 

1:创建时间不同

2:客户端激活可以用非缺省构造函数,服务端激活不支持非缺省够着

使用new操作符只是创建一个代理,并没有创建实际对象

客户端激活对象可通过new同时创建代理和对象

3:客户端和对象如何联系在一起的

WellKownObjectMode.SingleCall/Singleton(注意线程同步)

使用客户端激活远程对象时 该对象只为此客户端服务,可在多次调用间保持状态

例子:StopWatch 记录一个start 和一个stop的调用 查看每个客户端激活类型的差别