I'm getting data from a 3rd party web service using REST (using WebHttpBinding
and WebHttpBehavior
). When I pass an invalid parameter in the querystring, the service responds with a 500 status and describes the problem. The response looks something like this:
HTTP/1.1 500 Internal Server Error
Date: Tue, 14 Jun 2011 16:12:48 GMT
... more headers
<Error>Invalid integer value for ID</Error>
I would really like to be able to capture that error message. My WCF client looks like this:
public class DataClient : ClientBase<IDataClient>, IDataClient
{
public DataClient (string endpointAddress)
: base(new WebHttpBinding(), new EndpointAddress(endpointAddress))
{
this.Endpoint.Behaviors.Add(new WebHttpBehavior());
}
public string GetData(string id)
{
// This is where the exception is thrown
return base.Channel.GetData(id);
}
}
When I catch the exception that is thrown, that data is nowhere to be found. It just says, "System.ServiceModel.CommunicationException: Internal Server Error".
I tried creating a custom behavior and added a message inspector, but my custom message inspector code doesn't get executed before the exception is thrown (it does get executed if no exception is thrown).