2

When I have an exception in my code I want to return it as a FaultException/WebFaultException depending on if it is REST or SOAP. The following problem is with a REST component of my project. The Exception is triggered, I see it in my debug window (see picture).

enter image description here

We clearly see that the WebFaultException is being triggered, but my response in JSON is not a http badrequest nor is my exception message there, but always the following:

{"readyState":4,"responseText":"","status":202,"statusText":"Accepted"}

Here is my interface:

[WebInvoke(Method = "POST", UriTemplate = "/Package/", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
[FaultContract(typeof(FaultException))]
[OperationContract]
string CopyZip(Stream zippedPackage);

Here is its implementation:

public string CopyZip(Stream zippedPackage)
{
   try
   {
      ZipCreator pck = new ZipCreator();
      pck.CopyUploadedZip(zippedPackage);
      return "Zipped";
   }
   catch (Exception ex)
   {
   //throw new FaultException(ex.Message);
      throw new WebFaultException<string>(ex.Message,             HttpStatusCode.BadRequest);
   }

}

guiomie
  • 4,988
  • 6
  • 37
  • 67
  • 1
    Did you managed to resolve this problem ? I'm stuck like were (are). –  Jun 05 '15 at 15:40

1 Answers1

0

I've seen some posts where the issue was on the WcfRestContrib custom error handling. In those instances, changing the service class attribute from [ServiceConfiguration("Rest", true)] to [ServiceConfiguration("Rest", false)] is what ended up fixing the issue. Or, you could leave it as true, and try using a WebException rather than a WebFaultException, like this

throw new
    WcfRestContrib.ServiceModel.Web.Exceptions.WebException(HttpStatusCode.BadRequest, "My custom error!");
Chris Hobbs
  • 745
  • 2
  • 9
  • 27