I have a working WCF - WPF application working, however I\'m looking for some optimization. Below is my code where a WCF restful service is exposing a JSON array, and a WPF
Your JSON is getting double-serialized. (The extra backslashes in the JSON are symptomatic of this.)
Notice in the first version, you do not make a call to serialize the return object inside GetAllStatus()
, but in InvokeGet()
you do make a call to deserialize it. And that works. So, somehow the return object from GetAllStatus must be getting serialized automatically. It should be clear that WCF must be handling the serialization of your return object for you.
In the second version, you manually serialize the return object inside GetAllStatus()
using JsonConvert.SerializeObject()
. Since we just established that WCF is also doing serialization, you end up with double-serialized JSON.
To make it work, you need to ensure that the output is only serialized once. Presumably you had a reason to start using Json.Net in your service, so I would look for a way to replace the WCF serializer with JSON.Net. You don't have to look far-- there are quite a few questions and answers on StackOverflow that deal with this very topic. Here are few ideas: