一般情况下我们用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 对象,就必须在注册它们之前更改信道的名称。"
转载于:https://www.cnblogs.com/HurYun/archive/2006/10/19/533656.html