Fork me on GitHub
Remoting多个信道(Chennel)的注册问题

一般情况下我们用Remoting一个信道应该就够用了,因为程序要么是客户端,要么是服务器端。但是有时候也会出现一个客户端需要连接多个服务器端的情况,或者一个程序既作为服务器端(针对内网),又作为客户端(针对外网)。这个时候就需要注册多个信道了。
  根据一般的经验,客户端信道和服务器端信道应该是不冲突的。但实际的情况呢?看一下以下的代码:

 

IChannel serverChannel = new TcpServerChannel( 5000 ); 
ChannelServices.RegisterChannel( serverChannel, true ); 
 
IChannel clientChannel = new TcpClientChannel(); 
ChannelServices.RegisterChannel( clientChannel );
运行后会出现异常“信道 'tcp' 已注册。”(RemotingException)  

  注册两个客户端信道也一样会出现这个错误: 

 

IChannel channel1 = new TcpClientChannel(); 
ChannelServices.RegisterChannel( channel1, true ); 
IChannel channel2 = new TcpClientChannel(); 
ChannelServices.RegisterChannel( channel2, true );
开始我怀疑是端口冲突,给每个信道分别设置不同的端口:  

 

 

Hashtable props1 = new Hashtable(); 
props1["port"] = 5001; 
IChannel channel1 = new TcpClientChannel( props1, new BinaryClientFormatterSinkProvider() ); 
ChannelServices.RegisterChannel( channel1, true ); 
Hashtable props2 = new Hashtable(); 
props2["port"] = 5002; 
IChannel channel2 = new TcpClientChannel( props2, new BinaryClientFormatterSinkProvider() ); 
ChannelServices.RegisterChannel( channel2, true );

 

错误依旧。想想也是,如果端口冲突,应该是这种错误:“通常每个套接字地址(协议/网络地址/端口)只允许使用一次。”(SocketException) 
  再分析一下原来的错误:“信道 'tcp' 已注册。”。难道是信道的名字冲突? 
  赶紧把channel的ChannelName打印出来看一下: 
  Console.WriteLine( "The Default Channel Name is " + (new TcpClientChannel()).ChannelName ); 
  "The Default Channel Name is tcp"... 

  问题找到。接下来要做的就是在注册不同信道的时候,显式指定其信道名称。ServerChannel和ClientChannel各有不同的方法,以下示例其一:
IChannel channel1 = new TcpClientChannel( "Channel1", new BinaryClientFormatterSinkProvider() ); 
ChannelServices.RegisterChannel( channel1, true ); 
IChannel channel2 = new TcpClientChannel( "Channel2", new BinaryClientFormatterSinkProvider() ); 
ChannelServices.RegisterChannel( channel2, true );

BTW:因为很少看到网上Remoting的文章提到多信道的注册,所以把这个贴出来。也许大家注册信道的时候就指定了名字,这样就不会有这个问题。 另外,以上均是在.NET 2.0平台上。 

 

  MSDN上的相关说明:
  ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconchannels.htm
  "信道名称在应用程序域中必须是唯一的。例如,由于默认信道具有名称,因此,若要在一个应用程序域中注册两个 HttpChannel 对象,就必须在注册它们之前更改信道的名称。"

 就我所知,目前在Server端似乎还没有办法为WellknownServiceObject选择其使用的信道。.NET Remoting的通信信道的选择,好像是由底层自己进行的。而且Server端注册的ObjectUri,只是一个不完整路径,例如"Test",而在Client端使用的时候,则是完整Url路径:tcp://localhost:8086/Test。
 
  我所测试的代码里,通过Client激活对象时所指定的URL,可以选择远程通信所用的信道的类型。这主要通过其协议"tcp://"和端口"8086"来确定。
 
  也就是说,以下例子中,我们在服务器端注册的TestClass对象,无法确定其在tcp信道上还是在http信道上。一切都是根据客户端的具体请求来选择。

共享测试类如下:
namespace Share
{
    public class TestClass : MarshalByRefObject
    {
        public string Hello()
        {
            return "Hello from remoting!";
        }
    }
}

Server端使用这个配置文件,分别注册了一个tcp信道,一个http信道。远程对象只注册了一个用于测试的类。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel name="Channel1" port="8086" ref="tcp"/>
        <channel name="Channel2" port="8088" ref="http"/>
      </channels>
      <service>
        <wellknown mode="Singleton" type="Share.TestClass, Share" objectUri="Test"/>
      </service>
    </application>
  </system.runtime.remoting>
</configuration>

Server 代码:
RemotingConfiguration.Configure( "Server.exe.Config" );

 

Client端也注册了两个信道,tcp的和http的,配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel name="Channel1" port="8186" ref="tcp"/>
        <channel name="Channel2" port="8188" ref="http"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>
 
Client代码:
RemotingConfiguration.Configure( "Client.exe.Config" );
Share.TestClass test1 = (Share.TestClass) Activator.GetObject(
    typeof( Share.TestClass ), "tcp://localhost:8086/Test" );
Share.TestClass test2 = (Share.TestClass) Activator.GetObject(
    typeof( Share.TestClass ), "http://localhost:8088/Test" );
Console.WriteLine( test1.Hello() );
Console.WriteLine( test2.Hello() ); 

 

  http://www.cnblogs.com/kriss/archive/2005/11/30/288177.html

posted on 2010-05-18 22:58  HackerVirus  阅读(1178)  评论(0编辑  收藏  举报