title: Understanding ServiceKnownType in WCF
body: <p>I am having a little trouble understanding ServiceKnownType in WCF.</p>
  
  <p>Taken from <a href="http://blogs.msdn.com/sowmy/archive/2006/06/06/618877.aspx" rel="noreferrer">this blog</a>, the following code does not work:</p>
  
  <pre><code>[DataContract(Namespace = “http://mycompany.com/”)]
  public class Shape{…}
  
  [DataContract(Namespace = “http://mycompany.com/”)]
  public class Circle : Shape {…}
  
  [ServiceContract]
  public interface IMyServer
  {
      [OperationContract]
      bool AddShape(Shape shape);
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>IMyServer client = new ChannelFactory&lt;IMyServer&gt;(binding, endPoint).CreateChannel();
  
  client.AddShape(new Circle());
  </code></pre>
  
  <p>The reason it doesn't work is because you are trying to add a circle, but the servicecontract only allows a Shape. You are supposed to do something with knowntypes, but I am a bit confused about how that works.</p>
  
  <p>Since that code is in the service, why doesn't it know automatically that a Circle is derived from Shape? Additionally, what does ServiceKnownType actually do?</p>
  
  <p>When ServiceKnownType is put below the DataContract, apparently that makes it work. I am guessing it says hey, this particular object type called Shape can also be a Circle. I am having trouble understanding why it would do it this way around, because if you add a new type like Square you are going to have to add to the Shape class a ServiceKnownType for it. Wouldn't it make sense if it cannot infer it, to put the KnownType onto the Square rather than the Shape? So the Square says hey, I am a Shape, and you don't have to fiddle with the Shape class? If your Shape class is built into a library and you want to create your own dervied shape like DiamondShape, you can't add it to the Shape class because you don't have access to the source code.</p>