This project is read-only.

Exception Handling

Topics: Web Api
Nov 4, 2010 at 6:49 PM

While playing with some of these new features I got hung up with no response coming back from my service.  After opening fiddler to investigate I realized that I was recieving a 500 status code, but no bytes were coming back.  I tried adding [ServiceBehavior(IncludeExceptionDetailInFaults=true)] but I still didn't get any information back.

Is there a strategy for handling Exceptions?

Nov 4, 2010 at 8:50 PM

In the current prototype ServiceModel bits (Microsoft.ServiceModel.WebHttp) exceptions are not supported, though we are going to add this. One approach however is to not use exceptions at all as in HTTP you always need a response and a status code, there is not exception in HTTP. You can grab HttpResponseMessage as an input param and set the status code, content, etc to return something to the browser indicating an error like NotFound. If you check the ContactManager sample in the ContactResource, you will see we are doing this.

We are planning for compatibility to support exceptions. You will be able to soon throw a WebFaultException<T> passing in an HttpResponseMessage to set whatever you want on the response. Or just pass T as a string like you can do today. We will also trap unhandled exceptions and ensure that they turin into an HTTP response.

Does this make sense?

Nov 4, 2010 at 10:25 PM

This makes sense.  Thanks.