Remoting review 01

来源:互联网 发布:json对象添加到数组中 编辑:程序博客网 时间:2024/04/30 06:14

Remoting review

Remoting服务器端的三个步骤:

第一步:注册通道

TcpChannel channel=new TcpChannel(8080);

ChannelServices.RegisterChannel(channel);

第二步:注册远程对象

a.SingleTon模式:

RemotingConfiguration.RegisterWellKnowServiceType(typeof(ServerRemoteObject.ServerObject),"ServiceMessage",WellKnownObjectMode.SingleTon);

b.SingleCall模式:

RemotingConfiguration.RegisterWellKnowServiceType(typeof(ServerRemoteObject.ServerObject),"ServiceMessage",WellKnownObjectMode.SingleCall);

c.客户端激活模式:

RemotingConfiguration.ApplicationName="ServiceMessage";

RemotingConfiguration.RegisterActivatedServiceType(typeof(ServerRemoteObject.ServerObject));

第三步:注销通道

//获得当前已注册的通道

IChannel[] channels=ChannelServices.RegisteredChannels;

//关闭指定名为MyTcp的通道

foreace(IChannel eachChannel in Channels)

{

    if(eachChannel.ChannelName=="MyTcp")

    {

        TcpChannel tcpChannel=(TcpChannel)eachChannel;

        //关闭监听

        tcpChannel.StopListening(null);

        //注销通道

        ChannelService.UnregisterChannel(tcpChannel);

    }

}

 

 

Remoting客户端的两个步骤:

第一步:注册通道

TcpChannel channel=new TcpChannel();

ChannelServices.RegisterChannel(channel);

第二步:获得远程对象

a.WellKnown激活模式:

ServerRemoteObject.ServerObject serverObject=(ServerRemoteObject.ServerObject)Activator.GetObject(typeof(ServerRemoteObject.ServerObject),"tcp://locallhost:8080/ServiceMessage");

b.客户端激活模式,有两种方法:

b.1.

RemotingConfiguration.RegisterActivatedClientType(typeof(ServerRemoteObject.ServerObject),"tcp://locallhost:8080/ServiceMessage");

ServerRemoteObject.ServerObject serverObj=new ServerRemoteObject.ServerObject();

b.2.

object[] attrs={new UrlAttribute("tcp://locallhost:8080/ServiceMessage")};

object[] objs=new object[3];

objs[0]="wayfarer";

objs[1]="male";

objs[2]=28;

ServerRemoteObject.ServerObject serverObject=Activator.CreateInstance(typeof(ServerRemoteObject.ServerObject),objs,attrs);

该方法也可如下编写:

Object[] attrs={new UrlAttribute("tcp://locallhost:8080/EchoMessage")};

ObjectHandle handle=Activator.CreateInstance("ServerRemoteObject","ServerRemoteObject.ServerObject",attrs);

ServerRemoteObject.ServerObject obj=(ServerRemoteObject.ServerObject)handle.Unwrap();

 

结语:

第一点:Remoting的通道主要有三种:Tcp,Http和Ipc

第二点:Remoting中,远程对象的激活分为两大类:服务器端激活和客户端激活

(1)服务器端激活。

有叫做WellKnow方式,很多翻译为知名对象,为什么称为知名对象激活模式呢?是因为服务器应用程序在激活对象实例之前会在一个众所周知的统一资源标识符(URI)上来发布这个类型,然后该服务器进程会为此配置一个WellKnown对象,并根据指定的端口或地址来发布对象。

.Net Remoting把服务器端激活有分为SingleTon模式和SingleCall模式两种:

SingleTon模式:此为有状态模式。如果设置为SingTon激活方式,则Remoting将为所有客户端建立同一个对象实例。当对象处于活动状态时,SingleTon实例会处理所有后来的客户端访问请求,而不管它们是同一客户,还是其他客户端.SingleTon实例将在方法调用中一直维持其状态.

SingleCall模式:SingleCall是一种无状态模式。一旦设置为SingleCall模式,则当客户端调用远程对象的方法时,Remoting会为每一个客户端建立一个远程对象实例,至于对象实例的销毁则是由GC自动管理的.

(2)客户端激活。

与WellKnown模式不同,Remoting在激活每个对象实例的时候,会给每个客户端激活的类型指派一个URI。客户端激活模式一旦获得客户端的请求,将为每一个客户端都建立一个实例引用。SingleCall模式和客户端激活模式是有区别的:首先,对象实例创建的时间不一样。客户端激活模式是客户一旦发出调用的请求就实例化;而SingleCall则是要等到调用对象方法是再创建。其次,SingleCall模式激活的对象是无状态的,对象生命期的管理是有GC管理的,而客户端激活的对象则有状态,其生命期可自定义。其三,两种激活模式在服务器端和客户端实现的方法不一样。尤其在客户端,SingleCall模式是由GetObject()来激活,它调用对象默认的构造函数,而客户端激活模式,则通过CreateInstance()来激活,它可以传递参数,所以可以调用自定义的构造函数来创建实例。

第三点:Remoting可以注册多个通道

在Remoting中,允许同时创建多个通道,即根据不同的端口创建不同的通道。但是,Remoting要求通道的名字必须不同,因为它要用来作为通道的唯一标识符(使用System.Collection中的IDictionary接口来创建,不能使用IChannel来创建,因为它的ChannelName属性是只读的)

//注册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属性中,定义不同的通道名称就可以了。

原创粉丝点击