我正在从基于 C# 表单的应用程序调用 Web 服务。
ServReturnType obj = ServProxyClass(int i, int k);
我有可能获得例外。 例外情况可能是服务根本没有连接、接收失败但原始消息仍然存在,或者介于两者之间。 然后,我需要根据抛出的异常(类型或消息或两者)做出决定,以执行接下来的两个操作之一。
try
{
ServReturnType obj = ServProxyClass(int i, int k);
}
catch (WebException ex)
{
DoAction1();
}
catch(SocketException ex)
{
DoAction2();
}
catch(SoapException ex)
{
DoAction1()
}
问题是,我对后续操作的选择取决于第一次调用在失败之前是否到达服务器。 我如何找到所有可能抛出的异常的列表,以及它们可能意味着什么,因为这是框架中的所有代码。 我查看了 MSDN 文档,发现 SoapHttpClientProtocol.Invoke 可以抛出 SoapException,但是此方法调用的某些方法会引发 System.Net.WebExceptions。 所以我需要整个调用堆栈的列表以及它可以抛出什么,以及这意味着什么。
有任何想法吗?
更新1
达林,你的回答或多或少证实了我的怀疑,即我想要的东西并没有真正意义。 我选择先做更多的侦探工作,然后用它来决定下一步该做什么。 不幸的是,在这种情况下,由于我们正在处理信用卡,因此到底发生了什么以及我们是否发送数据确实很重要。
I am calling a web service from a C# forms based app.
ServReturnType obj = ServProxyClass(int i, int k);
I have the potential to get back an exception. The exception could be that the service didn't connect at all, that there was a failure on the receive, but the original message went, or anything in between. I then need to make a decision based on the exception thrown, either on the type, or the message, or both, as to do one of two actions next.
try
{
ServReturnType obj = ServProxyClass(int i, int k);
}
catch (WebException ex)
{
DoAction1();
}
catch(SocketException ex)
{
DoAction2();
}
catch(SoapException ex)
{
DoAction1()
}
The issues is, my choice of subsequent actions depends on whether or not the first call made it to the server before failing. How can I find a list of all possible exceptions thrown, and what they might mean, as this is all code in the framework. I looked that MSDN docs, and saw that SoapHttpClientProtocol.Invoke can throw a SoapException, however there are methods called by this method that bubble up System.Net.WebExceptions. So I need a list of the whole call stack and what it can throw, and what that means.
Any ideas?
Update 1
Darin, your answer more or less confirms my suspicions that what I want doesn't really make sense. I have opted to do more detective work first, then use that to decide what to do next. Unfortunately in this case, since we are processing credit cards, it really matters what exactly has happened, and whether or not we sent data.
发布评论
评论(1)
Web 服务调用可能引发的异常的完整列表是如此之大,以至于我真的不认为测试每种可能的异常类型是一个好主意。 在许多情况下,捕获 SoapException 就足够了,它可能包含通过 SOAP 错误元素 和所有其他异常都可以被视为未处理的错误:
这是一个很好的 文章 解释如何引发和处理 SoapException。
The complete list of exceptions that can be thrown from a web service call is so vast that I really don't think that it is a good idea to test for every type of exception possible. In many cases it would be enough to catch for SoapException which could contain some business errors transmitted through the SOAP Fault Element and every other exception could be considered as a non handled error:
Here's a nice article explaining how to raise and handle SoapExceptions.