Search code examples
silverlightrestsilverlight-5.0

Stop Silverlight 5 from throwing WebExceptions for non-200 status codes?


I am trying to develop a client application that calls a RESTful web service. As part of a RESTful design, the service uses a variety of HTTP status codes to communicate state back to the caller. For instance, if I request a resource that doesn't exist, the service responds with a 404 status code. Likewise, if I pass in malformed parameters, the service responds with a 400 (Bad Request) status code. Silverlight 5 automatically converts these into WebExceptions. Is there anyway I can get SL to not throw exceptions but return a legitimate response object with the status code, etc set to the what was received so that I can decide how to handle the response in my code?

(To further clarify, it appears this is only the case for status codes in the 400 and 500 ranges.)


Solution

  • AFAIK, there is no solution to avoid exceptions. Worse than that, it might be hard or even impossible to get the actual HTTP code (I'm not certain there, I haven't tried too hard).

    You'll probably want to develop an HTTP request tool that traps WebException and provide an error status to the caller.