How to totally control json serialization in WCF
I need to totally control the json serialization process in my Rest WCF service. I need to substitute the serialization result, that开发者_JS百科 is something similar to:
{ foo: 42, bar: 43 }
with:
myFunc( { foo: 42, bar: 43 } );
any ideas?
thanks m.
I had a similar issue previously, which I solved by returning a memory stream from a WCF service. once you've done that you can set the MIME type manually. this basically allows you to return any result as any MIME type. I think I used this for jsonp. Sorry, but I'm working off my iPad at the moment so I can't provide an example. A quick google search should get you what you need.
Also, I'd recommend using json.net for your serialization...it's much easier to work with than the standard .net serialized.
I think you need to create your own serializer. You inherit from DataContractSerializerOperationBehavior
and override CreateSerializer
.
You can see a sample of how it's done in protobuf.net.
It might be simpler to expand your output object to include a parameter which is the name of the calling function, and then invoking it on the callback on your web page.
{ "func": "myFunc", "foo": "42", "bar": "43" }
Other helpful links:
- Post from Rick Strahl from 2008 which might help you.
- WCF REST custom ResponseFormat
精彩评论