c# - WCF Service . Setting response type to XML/JSON using the Accept HTTP Header? -
i want wcf service accept , respond requests in json or xml. thought wcf supposed automatically interpret response type based on accept header client specifies. in client request specify accept header application/json receive xml response.
this service definition:
[operationcontract] [webinvoke(method = "post", uritemplate = "/getchecks", bodystyle = webmessagebodystyle.bare)] check[] getchecks(mycustomobj object);
im making request here :
using (webclient client = new webclient()) { client.headers["content-type"] = "application/json"; client.headers["accept"] = "application/json"; string response = client.uploadstring(endpoint, jsonrequeststring); // response xml }
i know can make 2 endpoints , specify 1 xml , other json id rather not this.
any ideas?
for have on server set property automaticformatselectionenabled true
you can either in config
<webhttpendpoint> <standardendpoint name="" helpenabled="true" automaticformatselectionenabled="true"/> </webhttpendpoint>
or in code:
var host = new servicehost(typeof (pricingservice)); var beh = new webhttpbehavior { automaticformatselectionenabled = true }; host.addserviceendpoint(typeof (ipricingservice), new webhttpbinding(), uri) .behaviors.add(beh);
Comments
Post a Comment