HTTP 404 Error manifests itself as EndPointNotFoundException on a WCF client
How can I make my client treat 404 Error correctly? Right now it catches a general exception...
My WCF Server uses WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound(); To return a 404 Error code
however my WCF client interperts it as an EndPointNotFoundException
There was no endpoint listening at http://myUrl that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The inner exception is WebException "The remote server return开发者_如何学Pythoned an error: (404) Not Found.
Here is what I found...
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
{
....
}
}
Old thread that I came accross on Google. If anyone is looking, then I think I've got a neater solution:
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
}
}
精彩评论