none
WebFaultException throwing a "not handled by user code" error and returning an HTTP 500 to the client. RRS feed

  • Question

  • I am trying to return a BadRequest and simple message when implentation throws an error in a WCF REST service, but the WebFaultExcption itself is throwing an exception and returning an HTTP 500 instead. Any ideas on why this might be happening?

    Here is a short code snippet:

    [WebDispatchFormatter]
    [WebHelp(Comment="Initiate an immediate or callback appoinment request")]
    [WebInvoke(UriTemplate = "InitiateContact", Method = Verbs.Post)]
    public string InitiateContact(AppointmentRqst rqst)
    {
         string ret = string.Empty;
         rqst.DeviceId = Helper.GetHttpAuthHdrDeviceId();

         try
         {
             ret = impl.InitiateContact(rqst);
         }

         catch (Exception ex)
         {
             throw new WebFaultException<string>(ex.Message, HttpStatusCode.BadRequest);
         }

         return ret;
    }

    Thursday, August 26, 2010 4:33 PM

Answers

  • Found the problem was related to WcfRestContrib custom error handling. Changing the service class attribute from [ServiceConfiguration("Rest", true)] to [ServiceConfiguration("Rest", false)]
    • Marked as answer by Bill2010 Friday, August 27, 2010 3:28 PM
    Thursday, August 26, 2010 10:19 PM

All replies

  • Found the problem was related to WcfRestContrib custom error handling. Changing the service class attribute from [ServiceConfiguration("Rest", true)] to [ServiceConfiguration("Rest", false)]
    • Marked as answer by Bill2010 Friday, August 27, 2010 3:28 PM
    Thursday, August 26, 2010 10:19 PM
  • Further update. In order to use WcfRestContrib custom error handling, you can change the above from "false" to "true", and then use WebException rather than WebFaultException as in the following:

    throw new WcfRestContrib.ServiceModel.Web.Exceptions.WebException(HttpStatusCode.BadRequest, "My custom error!");

    Friday, August 27, 2010 4:27 PM