HTTP 404错误在WCF客户端上显示为EndPointNotFoundException

时间:2010-05-19 19:02:35

标签: wcf http-status-code-404

如何让我的客户端正确处理404错误? 现在它抓住了一般的例外......

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

但是我的WCF客户端将其作为EndPointNotFoundException进行插入

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

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

2 个答案:

答案 0 :(得分: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
                {
                    .... 
                }
 }

答案 1 :(得分:0)

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