2010-05-19 48 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 
    } 
}