如何在不使用 try-catch 的情况下检测 WCF 连接?
我使用 WCF netNamedPipeBinding
和 wsHttpBinding
。我想知道单向方法是否可以在不尝试
的情况下成功执行。
编辑 正如有人指出的,连接性可以在给定时刻成功,但在下一个时刻失败。我不在乎。我只是想知道 WCF 是否有尝试的替代方案。有什么方法可以检查服务是否已启动并且可以访问吗?
I use WCF netNamedPipeBinding
and wsHttpBinding
. I would like to know whether a one-way method can execute successfully without try
ing it.
EDIT As someone noted the conectivity can succeed a given moment and in the next fail. I don't care. I just want to know if WCF has an alternative to try
ing. Is there any way to check if the service is up and reachable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,确定单向方法是否成功执行的唯一方法是它是否生成异常。
如果该方法不必是单向方法,您可以返回一个布尔值,该值表明该方法是失败还是成功。
我可以问一下你为什么要尝试这样做吗?如果不捕获方法上的异常,就无法判断异常是否发生时的错误情况。
如果我误解了您对“try ing”的使用,并且您的意思是在不实际执行该方法的情况下,您可以为该方法编写一个代理,该代理实际上并不提交操作,而仅执行处理步骤。
如果您这样做完全是为了测试目的,我建议您利用 Visual Studio 单元测试或将您的项目连接到 NUnit 中。
如果上述任一点未能解答您的问题,请提供更多有关您想要实现的目标的详细信息。
编辑:修改以供提问者澄清。
将帮助您查看连接当前是否打开,并在服务进入故障状态时通知您。这将预先让您知道呼叫是否会成功。但是,仍然有一些情况可能导致该特定方法失败。这只会在您可以成功调用服务方法时通知您。
To my knoweldge the only way to determine if a one-way method executed successfully is whether or not it generates an exception.
If the method does not HAVE to be a one-way method you can return back a boolean value that states whether or not the method failed or succeeded.
Might I ask as to why you are trying to do this? Without catching an exception on the method there is no way to tell of error conditions if an exception does occur.
If I'm misunderstanding your use of "try ing" it and you mean without actually executing the method, you can write a proxy for the method that doesn't actually commit the action, only does the processing step.
If you are doing this for entirely testing purposes I would like to recommend taking advantage of the Visual Studio unit tests or wiring your project into NUnit.
Please provide more details of what you are trying to accomplish if one of the points above didn't answer your question.
EDIT: Modified to account for clarification by asker.
Will cover you to see if the connection is currently open AND will notify you if/when the service goes into a faulted state. This will preemptively let you know whether or not the call will succeed. However, there are still conditions that may cause that specific method to fail.. This will only notify you if you can successfully call the service method.
如果您测试调用,它通过了,然后不知何故连接被断开并且您的下一个代码块失败了,会发生什么?您最好
尝试
它,然后在错误出现时处理它。What happens if you test the call, it passes, then somehow the connectivity is dropped and your next code block fails? You're better off
try
ing it, then handling the error if/when it appears.无法知道对单向操作的调用是否一定会成功;所有绑定都支持单向操作 (AFAIK)。
如果您想简单地确定调用是否是单向操作,那么您将必须迭代服务描述上的操作(您没有指示这是客户端还是服务器端调用)并查看是否操作是否是一种方式。
There's no way to know if a call to a one-way operation will be guaranteed to succeed; one-way operations (AFAIK) are supported on all bindings.
If you want to simply determine if the call is a one-way operation, then you will have to iterate through the operations on the service description (you aren't indicating if this is a client or server side call) and look at whether the operation is one way or not.