如何测试远程连接(检查状态)
我在主机应用程序中创建了一个对象,并且可以使用远程处理远程访问它,有什么方法可以测试连接以确保它仍然“活动”? 也许我可以使用一个在远程连接断开时触发的事件,或者一些可以告诉我远程连接状态的属性。 有这样的东西可用吗?
I have an object created in a host application and can access it remotely using remoting, is there any way I can test the connection to ensure it is still "alive"? Maybe an event I can use that fires if the remoting connection gets disconnected, or some property that can tell me the state of the remoting connection. Is there something like this available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常向远程服务器 MarshallByRef 类添加另一个方法,
(我通常将其命名为 Ping(),如:
不执行任何操作,也不返回任何内容。然后为了“测试”我的连接,我调用此方法...如果它抛出 System.Net.Sockets.Exception,我有失去了连接......
I generally add another method to the remoting server MarshallByRef class,
(I generally name it Ping(), as in:
that does nothing, and returns nothing.. Then to "test" my connection, I call this method... If it throws a System.Net.Sockets.Exception, I have lost the connection....
检查连接有什么好处? 即使您对它进行 ping 操作,也不意味着在您进行远程调用时连接仍然处于活动状态。
只要尝试/捕捉您的远程呼叫,您就会知道。
这种类型的检查是没有意义的(网络连接、文件锁定等)。 因为您检查的事物的状态可能会在检查后立即发生变化。 你只需托盘,如果失败则清理/重试。
What benefit will you have to check the connection? Even if you ping it, it does not mean that in the next moment the connection will still be alive when you make your remoting call.
Just try/catch you remoting calls and you will know.
This type of checks are meaningless (net connection, file locking, etc.). As the state of the thing you check can change immediately after the check. You just tray, and cleanup/retry if it fails.