3

How can I create the service contract to be in XmlSerializerFormat as well as WebMessageFormat.Json within a WCF RESTful service.

What I need is to call the "CallADSWebMethod" operation contract from code behind of ASP.Net 1.1 which needs to be XML serialized and from jQuery ajax which is Json serialized.

SERVICE CONTRACT

[ServiceContract, XmlSerializerFormat] public interface IService { [OperationContract, XmlSerializerFormat] [WebInvoke(UriTemplate = "/CallADSWebMethod", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)] VINDescription CallADSWebMethod(string vin, string styleID); } 

Endpoint Info

 <endpoint address="basic" binding="basicHttpBinding" name="httpEndPoint" contract="ADSChromeVINDecoder.IService" /> <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="webBehavior" name="webEndPoint" contract="ADSChromeVINDecoder.IService" /> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
5
  • 1
    Suggest you go through this: <stackoverflow.com/questions/1008706/…> Commented Oct 18, 2012 at 5:20
  • 1
    I would try by adding behaviorConfiguration="webHttpEndpoint" to json endpoint and inserting this <endpointBehaviors> <behavior name="webHttpEndpoint"> <webHttp faultExceptionEnabled="true" defaultOutgoingResponseFormat="Json" defaultBodyStyle="WrappedRequest" automaticFormatSelectionEnabled="True" /> </behavior> </endpointBehaviors> Commented Oct 18, 2012 at 7:03
  • 1
    @L.B - I did added that, but than the "XmlSerializerFormat" in service contract is causing problem and showing..Operation 'CallADSWebMethod' (in contract 'IService' with namespace 'tempuri.org/') specifies the http body format to be JSON but the serialization mechanism for the operation is not DataContract. JSON format is only supported with DataContract serialization. If you have an XmlSerializerFormatAttribute on the operation consider replacing it with DataContractFormatAttribute Commented Oct 18, 2012 at 7:12
  • Do you mean you would like the service to return VINDescription object is both XML and JSON formats as response as needed? Commented Oct 18, 2012 at 10:32
  • Just remove the response format setting from the WebInvoke attribute and then the WCF framework will decide on which format to send the data back to client based on the Accept http header value. By default its XML format. Commented Oct 18, 2012 at 10:36

2 Answers 2

2

What you can do is specify your Web Service like this:

 [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = ""/CallADSWebMethod"")] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = ""/CallADSWebMethod"")] VINDescription CallADSWebMethod(string vin, string styleID); } 

However, what I would suggest you to do is specify 2 different endpoints: one for the XML serialized data and another for JSON serialized data. Come on dude, you are using the REST architecture.....why not make full use of it??!

Sign up to request clarification or add additional context in comments.

2 Comments

I do have 2 endpoints..but it was not allowing for 2 different formats.
Can you have two WebInvoke attributes with the same method ? I get a compilation error "Duplicate WebInvoke Attribute"
1

This can actually be done without a dual declaration as shown in this answer by setting the webHttpBehavior's automaticFormatSelectionEnabled property to true.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.