views:

1071

answers:

3

I have an existing web service that returns XML responses and I would like to add some new methods that return JSON. Do I have to create a separate web service that returns in JSON or can I have a mix?

If I use the ResponseFormat = WebMessageFormat.JSON I need to have the service annotated with [DataContractFormat] but I cant seem to have that and [XmlSerializerFormat] which is required for the xml type response format.

+1  A: 

I don't see why this isn't possible. You annotate the service with the [ServiceContract] attribute (not DataContractFormat). It should look like

 [ServiceContract]
    public interface IDoStuff
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
             UriTemplate = "DoStuff",
             ResponseFormat = WebMessageFormat.Json,
             RequestFormat = WebMessageFormat.Json)]
        TestObj DoWork(TestInputObj Inp);
    }

To make it xml, just change the responseformat. When you do your post command, you'll get json, a separate method with the xml format would give you xml.

Steve
+1  A: 

Do you know for a fact that you need XmlSerializerFormat? That's only needed if you need to serialize to XML that doesn't comply with the rules for the Data Contract Serializer.

If you do need that, then you do need a second service. That's pretty easy though. Just extract the guts of the operations in common into separate methods, then call those methods from both services.

John Saunders
A: 

I believe you are reffering to XMLSerilization attribute in paste as xml type. If so it wont affect your json data. you can use that to serilize both XML & Json. response.Content.ReadAsJsonDataContract()/ReadAsXmlDataContract() where T being for XMLSerilizable type.

PradeepN