3

Here's my service code. I throw an error just like all the best-practice articles suggest - in a form of WebFaultException.

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class API
{
    [OperationContract]
    [WebGet()]
    public int MyMethod()
    {
        throw new WebFaultException<string>("TESTERROR", HttpStatusCode.BadRequest);
    }
}

Now when a send a request to http://localhost:1389/API.svc/MyMethod all I get is this JSON object:

{"ExceptionDetail":null,"ExceptionType":null,"Message":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.","StackTrace":null}

I tried enabling includeExceptionDetailInFaults in my web.config, the message does change a little, but still I don't see my "TESTERROR" anywhere!

3 Answers 3

1

I think what's happening is that you don't have any sort of return happening, only a response code. So your server is throwing a 500 without any additional info. You can still use this by having the exception details output to a log file.

What I usually do is create a serializable response object that contains a message and any codes that I want returned to the client. Then I catch the exception, build the response object and return that to the client. The client then receives an xml payload that it can read from. So if it's anything other than 200 (OK), then I consider it a fault and display the appropriate message (or details that I included in the xml.

<ServiceError>
   <ServerCode>
      500
   </ServerCode>
   <ApplicationCode>
      9100
   </ApplicationCode>
   <Message>
      API key is expired.
   </Message>
</ServiceError>

UPDATE:

As far as JSON standard goes, I've never seen an actual document standard. What I HAVE seen is a sort of defacto standard, or rather a trend which is:

{
  "status": "success", //or "failed"
  "data": {
    //any app specific payload here
  },
  "message": null //or additional info here (i.e. exception details)
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know I can construct anything I want, using HttpContext.Current for example... but what us THE DOCUMENTED way to return errors in a JSON RESTful WCF service?
I don't think there is an official standard response. But there is a common response (edited my answer)
yap, thanks for the edit, but I meant - if there's any built-in mechanism in .NET that allows returning this kind of data. Guess not... Ok, I'll wait a couple of days before closing the question thanks
1

Ok, turns out you have to add this to the .svc-file: Factory="System.ServiceModel.Activation.WebServiceHostFactory"

And remove the <behaviors> section from web.config, leaving only this:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
        <service name="MyAPI"><endpoint address="" binding="webHttpBinding"/></service>
    </services>
</system.serviceModel>

Comments

0

I fall to the same problem and debugged wcf library, ExeptionDetail comes from these lines in WebScriptEnablingBehavior:

ExceptionDetail originalFaultDetail = fault.GetDetail<ExceptionDetail>();

It tries to deserialize error you set to ExceptionDetail type object and fails. Use the type ExceptionDetail while creating WebFaultException:

throw new WebFaultException<ExceptionDetail>(new ExceptionDetail(new Exception("TESTERROR")), HttpStatusCode.BadRequest);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.