• λ我爱Aspx >> Asp.Net >> Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇
  • Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇

  • :aspxer  Դ:internet  :2007-5-15 20:46:10  ؼ:.net
  • 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;

    Ҷƪл˵?
  • һƪMicrosoft .Net Remoting系列专题之三:Remoting事件处理全接触
    һƪ一步一步学Remoting之四:承载方式