I'm using the [WebGet] attribute to query a REST API on a web server. Every so often (because the API is still under development) I get back an HTML document in the form of an HTTP/500 error instead of the XML I want.
Here's an example of my operation contract:
[WebGet(UriTemplate = "search/?api_key={key}&query={query}",
BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
XElement Find(string key, string query);
... and here's how I call it:
var factory = new WebChannelFactory<IFooFinder>(
new Uri("http://api.example.com"));
var channel = factory.CreateChannel();
var results = channel.Find(myApiKey, "foo");
In the event of an error, "results" ends up being an XElement that contains this XML:
<html>
<head></head>
<body>
500 Internal Server Error
</body>
</html>
Is there some way to catch the 500 error before the XML is returned, and have the channel throw an exception or something? Or am I going to have to check the results variable each time to make sure the XML I expect is in there?