wcf - Applying [WebInvoke(ResponseFormat = WebMessageFormat.Json)] in config file -
i'm writing i'm referring poj (plain old json) wcf web service - 1 takes , emits standard json none of crap asp.net ajax likes add it.
it seems there 3 steps accomplish this:
- change "enablewebscript" "webhttp" in endpoint's tag
- decorate method [webinvoke(responseformat = webmessageformat.json)]
- add incantation of [aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] service contract
this working ok me - can pass in , being returned nice plain json.
if remove webinvoke attribute, xml returned instead, doing supposed do. strikes me odd option specify json output appears here , not in configuration file. wanted expose method xml endpoint - how this? way can see have second method same thing not have webmethodformat.json specified. rinse , repeat every method in service? yuck.
specifying output should serialized json in attribute seems contrary philosophy of wcf, service implemented transport , encoding agnostic manner, leaving nasty details of how data moved around configuration file.
is there better way of doing want do? or stuck awkward attribute? or not understand wcf enough?
i haven't tested out yet, but, took @ webmessageformat in reflector , used in code.
there attribute of webhttp element called defaultoutgoingresponseformat can set "json" or "xml".
<behaviors> <endpointbehaviors> <behavior name="servicesjsonendpointbehavior"> <webhttp defaultoutgoingresponseformat="json"/> </behavior> </behaviors>
i've run same issue , typically crufted work-arounds after searching online without info.
i'll give shot multiple configured endpointbehaviors , report back.
update 6/5/2011
fyi -- i've ditched vanilla wcf hair-pulling scenarios should simple, in favor of servicestack ( http://servicestack.net/ ). if you're looking put standard rest style / document-oriented service on http out-of-the-box supports json / xml / csv (and in future protocol buffers) , allows put clean mvc-style routes ease, give servicestack hard look. there number of things servicestack handle rather , cleanly turn out major pita due wonky configuration issue or similar in standard wcf. servicestack uses it's own json serializer, added bonus, outperforms datacontractjsonserializer , json.net demis mentions below.
Comments
Post a Comment