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" />
Christian
  • 997
  • 3
  • 14
  • 29
James
  • 1,826
  • 3
  • 20
  • 42
  • 1
    Suggest you go through this: – Sayan Oct 18 '12 at 05:20
  • 1
    I would try by adding `behaviorConfiguration="webHttpEndpoint"` to json endpoint and inserting this ` ` – L.B Oct 18 '12 at 07:03
  • @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 'http://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 – James Oct 18 '12 at 07:12
  • Do you mean you would like the service to return VINDescription object is both XML and JSON formats as response as needed? – Rajesh Oct 18 '12 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. – Rajesh Oct 18 '12 at 10:36

2 Answers2

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??!

Sayan
  • 1,958
  • 3
  • 24
  • 36
  • I do have 2 endpoints..but it was not allowing for 2 different formats. – James Oct 18 '12 at 06:48
  • 1
    Can you have two WebInvoke attributes with the same method ? I get a compilation error "Duplicate WebInvoke Attribute" – neildt Feb 14 '14 at 09:59
1

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

Community
  • 1
  • 1
Steve Westbrook
  • 1,670
  • 2
  • 20
  • 21