如何在.NET中删除TcpChannel对象
我在使用 TcpChannel
时遇到了一些问题。我想创建一个通道,对一个对象(比如说服务器)进行远程访问,完成所有这些操作后,关闭该通道。问题是我可能需要稍后在同一个端口重新打开同一个通道,而我在尝试执行此操作时遇到了一些困难。
出于连接目的,我只这样做:
var channel = new TcpChannel(port);
Console.WriteLine("Start Connection received at Server");
ChannelServices.RegisterChannel(channel, false);
//Initiate remote service as Marshal
RemotingServices.Marshal(this, "Server", typeof(Server));
然后要关闭它,我只这样做:
Console.WriteLine("Stop Connection at Server");
channel.StopListening(null);
RemotingServices.Disconnect(this);
ChannelServices.UnregisterChannel(channel);
channel = null;
此后,如果我尝试创建一个新的 tcpChannel 实例,我会收到一个异常,说 tcpChannel 连接是唯一的,它们必须位于不同的端口上。
那么,我怎样才能关闭tcpChannel呢? :S
提前致谢。
I'm having some troubles with TcpChannel
. I want to create a channel, give remote access to an object, let's say, a server and after doing all this, close the channel. The problem is that I might need to re-open the same channel later, in the same port, and I'm having some hard time trying to do this.
for connection purposes I only do:
var channel = new TcpChannel(port);
Console.WriteLine("Start Connection received at Server");
ChannelServices.RegisterChannel(channel, false);
//Initiate remote service as Marshal
RemotingServices.Marshal(this, "Server", typeof(Server));
then to close it, i just do:
Console.WriteLine("Stop Connection at Server");
channel.StopListening(null);
RemotingServices.Disconnect(this);
ChannelServices.UnregisterChannel(channel);
channel = null;
After this if I try to create a new tcpChannel instance, i get an exception saying that the tcpChannel connections are unique, they must be on different ports.
So, how can i close the tcpChannel? :S
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的关闭代码正在运行。
重新检查日志,您在某处错过了“在服务器上停止连接”。
更新:
我的日志(没有错误):
启动服务器收到的连接
停止服务器连接
启动服务器收到的连接
Stop Connection at Server
那里的实现代码:
your close code are working.
recheck the logs, you miss the "Stop Connection at Server" somewhere.
Update:
there the my log (no errors):
Start Connection received at Server
Stop Connection at Server
Start Connection received at Server
Stop Connection at Server
there the implementation code:
如果您只想停止并开始在同一端口上侦听,则需要显式调用 开始收听。您可以在 StopListening 之后松开代码的最后三行,并保留并重用该对象,直到您的应用程序出现故障。
If you just want to stop and start listening on the same port you need to explicitly call start listening. You can loose the last three lines of the code after StopListening and preserve and reuse the object until your application goes down.
您需要将通道属性:exclusiveAddressUse 设置为 false。
You will need to set the channel property: exclusiveAddressUse to false.