【问题标题】:HTTP 404 Error manifests itself as EndPointNotFoundException on a WCF clientHTTP 404 错误在 WCF 客户端上表现为 EndPointNotFoundException
【发布时间】:2010-05-19 19:02:35
【问题描述】:

如何让我的客户正确处理 404 错误? 现在它捕获了一个一般异常......

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

但是我的 WCF 客户端将其解释为 EndPointNotFoundException

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

内部异常是 WebException "远程服务器返回错误:(404) Not Found。

【问题讨论】:

    标签: wcf http-status-code-404


    【解决方案1】:

    这是我发现的……


    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
                    {
                        .... 
                    }
     }
    

    【讨论】:

    【解决方案2】:

    我在 Google 上遇到的旧线程。如果有人在看,那么我想我有一个更简洁的解决方案:

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多