2010-05-19 122 views
0

如何让我的客户端正确处理404错误? 现在抓到一个一般例外...HTTP 404错误在WCF客户端上显示为EndPointNotFoundException

我的WCF服务器使用 WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound(); 返回404错误代码

但是我的WCF客户端interperts它作为一个EndPointNotFoundException

有没有终点在http://myUrl是可以接受的消息听。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。

内部异常是引发WebException “远程服务器返回错误:(404)未找到

回答

0

这是我发现的...


catch (EndpointNotFoundException exception) 
{ 

       if (exception.InnerException.GetType() == typeof(WebException)) 
       { 
        WebException webException = (WebException) exception.InnerException; 
        if (webException.Status == WebExceptionStatus.ProtocolError) 
        { 
         if (((HttpWebResponse) webException.Response).StatusCode == HttpStatusCode.NotFound) 
         { 
          ... 
         } 

        } 
       } 
       else 
       { 
        .... 
       } 
} 
+0

*** substatusco de ***不可用:https://msdn.microsoft.com/en-us/library/system.web.httpresponse.substatuscode(v=vs.110).aspx – Kiquenet 2015-04-14 09:20:23

0

旧线,我如果有人正在寻找,那么我认为我有一个整洁的解决方案:

try 
{ 
    //operation that throws the Exception 
} 
catch (EndpointNotFoundException e) 
{ 
    WebException w = e.InnerException as WebException; 

    if (w != null) 
    { 
     HttpWebResponse resp = w.Response as HttpWebResponse; 
     if (resp != null && resp.StatusCode == HttpStatusCode.NotFound) 
     { 
      //The error was a 404 not found 
     } 
     else 
     { 
      //The response was null, or the error was not a 404 
     } 
    } 
    else 
    { 
     //The InnerException was not a WebException 
    } 
}