locked
Customizing Web Service XML Communication RRS feed

  • Question

  • Hi there,

    I want to develop a Web Service communicating with a custom protocol based on XML and REST. Basically, i want to be able to define how the objects are converted into XML.

    Is this possible using WCF?

    Thanks,

    Luis.

    Friday, April 30, 2010 2:09 PM

Answers

All replies

  • You can use XMLSerialiser to your contracts which have more control over your classes on serializing and deserializing, then the datacontract serializer, In addition to that you have messagecontracts where in you specfiy the header and body parts of the messages,

    So to controls header and body, use the messagecontracts , to control the contracts/classes to convert the properties into xml attribute / xml element, use the xml serialiser, here the example

     [XmlType]
     public class Person
     {
      [XmlElement]
      public int Age { get; set; }
    
      [XmlAttribute]
      public string Name { get; set; }
     }
    
     [MessageContract]
     public class Employee
     {
      [MessageHeader]
      public string category { get; set; }
    
      [MessageBodyMember]
      public Person Details { get; set; }
     }

    Please mark as answer, if it solves your problem, Vote if it helps your problem
    • Marked as answer by Mog Liang Friday, May 7, 2010 10:19 AM
    • Unmarked as answer by Luís Cruz Monday, May 10, 2010 2:10 PM
    Friday, April 30, 2010 4:47 PM
  • The answer was quite confusing...

     

    From what i understood from webservices, the communication message is returned in a special XML format; we have to redefine that XML format to attend to a specific protocol. 

    It's something like this:

    <feed xmlns:sdata="http://schemas.sage.com/sdata/2008/1"
     xmlns:http="http://schemas.sage.com/sdata/http/2008/1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.w3.org/2005/Atom">
     
     <!-- Atom feed elements -->
     <title>Sage App | Sales Orders</title>
     <id>https://interop.sage.com/sdata/accounts50/SDO/-/customer(`CUST001’)
     /salesOrders</id>
     <updated>2009-09-24T13:46:45Z</updated>
     <author>
     <name>sageApp</name>
     </author>
     
     <!-- Feed level links ... -->
     <!-- Categories ... -->
     <!-- Paging information ... -->
     <!-- Simple List Extensions ... -->
     <!-- Atom entry -->
     <entry>
     <id>http://www.example.com/sdata/sageApp/test/-/salesOrders('43660')
     </id>
     <title>Sales Order 43660</title>
     <updated>2009-09-24T13:46:45Z</updated>
     <link rel="self" type="application/atom+xml; type=entry" title="self"
     href="https://interop.sage.com/sdata/accounts50/SDO/-/salesOrders
     ('43660')" />
     <category scheme="http://schemas.sage.com/sdata/categories"
     term="resource" label="Resource"/>
     <content>Sales order 43660: amount 1553,1035</content>
     
     <!-- Resource payload -->
     <sdata:payload>
     <salesOrder sdata:key="43660" xmlns="http://schemas.sage.com/demoErp">
     <orderDate>2001-07-01</orderDate>
     <shipDate xsi:nil="true" />
     <orderLines sdata:url="https://interop.sage.com/sdata/accounts50/SDO/
     -//salesOrderLines?where=salesOrderID%20eq%2043660"/>
     <subTotal>1553.1035</subTotal>
     </salesOrder>
     </sdata:payload>
     <http:etag>gJaGtgHyuAwW6jMI4i0njA==</http:etag>
     </entry>
     
     <!-- ... 8 more entries ... -->
     
     </feed>

    Best regards,

    Luís Cruz

     

    Monday, May 10, 2010 2:24 PM
  • Hi Luis,

    As other member suggested, WCF can use XmlSerializer for serilize .NET object into XML content(when tranfered over wire). Also, by default WCF use DatacontractSerializer, which doesn't have full control over object ---XML convertion, so you can manually swtich your service to use XML serializer.

    #Using the XmlSerializer Class
    http://msdn.microsoft.com/en-us/library/ms733901.aspx

    #XmlSerializer vs DataContractSerializer: Serialization in Wcf
    http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/

    Also, as you mentioned that the target service is REST based( seems it is a RSS like service from the sample XML you provided). Thus, you can also consider define your WCF service/client as REST based(use webhttpBinding) which can send or receive data as raw binary stream. And you can read the stream in any arbitrary format(binary or xml or text ) as you like. Here are the MSDN reference which mentioned this:

    #How to: Create a Service That Accepts Arbitrary Data using the WCF REST Programming Model
    http://msdn.microsoft.com/en-us/library/cc656724.aspx

    #How to: Create a Service That Returns Arbitrary Data Using The WCF Web HTTP Programming
    http://msdn.microsoft.com/en-us/library/cc681221.aspx

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, May 17, 2010 3:49 AM