How to make custom WCF error handler return JSON response with non-OK http code?

前端 未结 8 1617
春和景丽
春和景丽 2020-12-04 12:36

I\'m implementing a RESTful web service using WCF and the WebHttpBinding. Currently I\'m working on the error handling logic, implementing a custom error handler (IErrorHand

8条回答
  •  孤城傲影
    2020-12-04 12:55

    Double-check that your errorObject can be serialized by DataContractJsonSerializer. I ran into a problem where my contract implementation didn't provide a setter for one of the properties and was silently failing to serialize--resulting in similar symptoms: 'server did not send a response'.

    Here's the code I used to get more details about the serialization error (makes a good unit test with an assertion and without the try/catch for breakpoint purposes):

    Stream s = new MemoryStream();
    try
    {
        new DataContractJsonSerializer(typeof(ErrorObjectDataContractClass)).WriteObject(s, errorObject);
    } catch(Exception e)
    {
        e.ToString();
    }
    s.Seek(0, SeekOrigin.Begin);
    var json = new StreamReader(s, Encoding.UTF8).ReadToEnd();
    

提交回复
热议问题