Vb.Net异常过滤CommunicationException与ServiceModel.FaultException

时间:2017-07-19 19:21:50

标签: .net vb.net exception-handling

好的,所以我试图在程序中添加一些异常处理并得到一些奇怪的行为。我添加了以下Catch:

Catch ex As Exception When TypeOf ex Is IOException _
    OrElse TypeOf ex Is TimeoutException _
    OrElse TypeOf ex Is ServiceModel.EndpointNotFoundException _
    OrElse TypeOf ex Is ServiceModel.ServerTooBusyException _
    OrElse TypeOf ex Is ServiceModel.CommunicationException

当以下异常落入这段代码时,我感到很惊讶:

System.ServiceModel.FaultException: System.Web.Services.Protocols.SoapException: There was an exception running the extensions specified in the config file. ---> System.Web.HttpException: Maximum request length exceeded.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.get_InputStream()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

当我调试这个条件TypeOf ex是ServiceModel.CommunicationException是True,但是我无法在stacktrace或exception Message中的任何地方找到该异常。任何人都能解释一下吗?我正试图捕捉“基础连接已关闭:......”或“现有连接被远程主机强行关闭”等内容

1 个答案:

答案 0 :(得分:1)

FaultException继承自CommunicationException,如果“objectexpression属于typename类型或继承自typename”,则TypeOf运算符返回true,因此在这种情况下{{1}是正确的}}