gpt4 book ai didi

c# - 生成的服务引用可能抛出哪些异常?

转载 作者:行者123 更新时间:2023-12-04 03:22:41 24 4
gpt4 key购买 nike

我已使用“添加服务引用...”在Visual Studio 2010中添加了Web服务。这会在名为Reference.cs的文件中生成一些代码。现在,如果我调用其中一种方法,我将不知道该方法可能引发哪些异常。大概可以抛出与网络相关的异常,例如SocketExceptionIOException吗?

可以在msdn或源代码内部检查.NET中的常规方法,以揭示可能引发了哪些异常,例如File.Open。在这里,很清楚我应该捕获并重新抛出哪些异常以在以后的阶段显示错误消息。

对于这些生成的方法,我如何知道它们可能引发哪些异常?

最佳答案

嗯,在这种情况下,存在“标准”异常和“自定义”异常(服务开发人员定义为FaultContact,并出现在服务契约(Contract)引用中的那些异常(exception))。

我想,在第一种情况下,您的关注点是 CommunicationException TimeoutException ;这些已记录为ICommunicationObject.BeginOpen ICommunicationObject (base of the model)的其他“打开”方法的可能异常(exception)。 CommunicationObjectFaultedException 被记录为“关闭”方法。还有用于发送消息的方法的 QuotaExceededException ,例如 IRequestChannel.Request 。在many more that might be中,这些应该是可发现的。

值得注意的是,从上面链接的MSDN文章中,这是:

All exceptions thrown by channels must be either a System.TimeoutException, System.ServiceModel.CommunicationException, or a type derived from CommunicationException. (Exceptions such as ObjectDisposedException may also be thrown, but only to indicate that the calling code has misused the channel. If a channel is used correctly, it must only throw the given exceptions.)



然后是“故障”,这是在服务端引发的异常,并且(可能的话,如果启用了)向调用者详细说明,则调用者可以处理该异常或抛出适当的客户端异常:

When generating a fault, the custom channel should not send the fault directly, rather, it should throw an exception and let the layer above decide whether to convert that exception to a fault and how to send it.



channel State提供了 Faulted事件,您可以订阅该事件,以便在达到这种状态时通知您,甚至可以采取行动。默认情况下(不配置supression(?)),错误将作为托管异常引发。再次重申:

In WCF clients, SOAP faults that occur during communication that are of interest to client applications are raised as managed exceptions. While there are many exceptions that can occur during the execution of any program, applications using the WCF client programming model can expect to handle exceptions of the [...] two types as a result of communication.



以及 this refers again至上述的 CommunicationExceptionTimeoutException

最后,至少就目前而言,是出乎意料的:

FaultException exceptions are thrown when a listener receives a fault that is not expected or specified in the operation contract; usually this occurs when the application is being debugged and the service has the System.ServiceModel.Description.ServiceDebugBehavior.IncludeExceptionDetailInFaults property set to true.

关于c# - 生成的服务引用可能抛出哪些异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10209025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com