try
{
///// here is the code that might throw erros. If I discover the user is unauthorized I throw a WebFaultException myself to alert the client
}
catch (WebFaultException ex)
{
throw ex; //but if I wrap all code in try-catch, I have to rethrow the exception o the status code reaches the client
}
catch (Exception ex)
{
throw new WebFaultException(ex.Message, HttpStatusCode.InternalServerError);
}
Should I wrap all in a try-catch, or what do you recommend? I use WCF with rest-ful JSON services
you could do, but it would probably be better to implement IErrorHandler and add it as a behaviour to your service, which will allow your unhandled exceptions to be handled in a single place, so you’ll be able to create a fault exception there to return details to the users.
then to create a behaviour which uses this:
Then if you are self hosting you can just add the behaviour programmatically:
if you want to add it via configuration then you need one of these:
and then the configuration: