title: Where do I hook into a WCF SOAP response to generate the XML for the object being returned?
body: <p>Say I have a Person object that has a first name, a last name and an age. I want to send this person object to a web service that only knows and cares about a person object with a first and last name. But I want the xml that represents the full person object including the age to be sent back. </p>
  
  <p>I already have a mechanism for storing the xml in the object behind the scenes and can simply serve that xml up to WCF, but I don't want to use the normal XmlSerializer. I want to be able to say "here is the xml. wrap it in soap and send it". </p>
  
  <p>I plan on using WCF. Where would I hook into this process to control the serialization?</p>
  
  <h1>More Details</h1>
  
  <p>While the example in this question is contrived. It clearly illustrates my problem. Lets say I have an application with a class like so:</p>
  
  <pre><code>/* person A */
  class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
  }
  </code></pre>
  
  <p>I am passing this to a web service. The web service knows about a different Person class written as:</p>
  
  <pre><code>/* person B */
  class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
  }
  </code></pre>
  
  <p>The service has a method with the following method signature:</p>
  
  <pre><code>Person SomeOperation(Person p);
  </code></pre>
  
  <p>When I call this web service from my application and pass it my person A version, the web service receives it just fine. It ignores the Age property. But when it recreates the response the Age property disappears.</p>
  
  <p>I understand <em>why</em> this is happening. WCF is deserializing the response into an actual c# object so the extra XML that was sent with the request is discarded. Because of this, there is no Age property to send back.</p>
  
  <p>I have a mechanism to actually use the XML as the backing store for my objects. If there is XML in the backing store that the object does not need, it just sits there and is not used. I want the full XML used as the backing store to be sent back in the response.</p>
  
  <p>If this were a basic c# application I could simply create an IBasicPerson interface that contains the FirstName and LastName property and have a Person class implement the interface like the below code:</p>
  
  <pre><code>public interface IBasicPerson {
    string FirstName { get; set; }
    string LastName { get; set; }
  }
  
  public class Person : IBasicPerson {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
  }
  </code></pre>
  
  <p>then I could create an operation in some other class that takes and returns the IBasicPerson interface:</p>
  
  <pre><code>public SomeClass {
    public IBasicPerson SomeOperation(IBasicPerson p) {
      /* do stuff */
      return p;
    }
  }
  </code></pre>
  
  <p>When i call this, it doesn't care about the Age property and the information does not get lost. This is because both the IBasicPerson in SomeOperation and the actual Person instance passed in refer to the exact same object. However, this is not the case when making WCF calls across the wire.</p>
  
  <p>From the application calling the WCF services point of view the object in the return value of the method is not the same object that was passed in.</p>
  
  <p>My XML backing store solves this problem, but only if I can figure out how to keep it from getting lost. This is why I'm trying to figure out where I should hook into the WCF serialization process. </p>