title: WCF Exception: Cannot add to list of known types
body: <p>Good Day Everyone...</p>
  
  <p>I’m getting an unexpected WCF error complaining of “Known Types” which are similarly named, but in different name-spaces (see error below).</p>
  
  <p><em>...please keep in mind I'm still experimenting with all this.</em></p>
  
  <p>Also…</p>
  
  <p>I already understand the nature of regular web-services FORCES you to flatten your object hierarchy, but everything I’ve read says you don’t have to do that with WCF…which is why I’m attempting this.</p>
  
  <p><strong>So my questions are…</strong></p>
  
  <ul>
  <li>Q: Is it possible to tell WCF to NOT TO IGNORE
  the name-spacing?</li>
  <li>Q: Am I missing a tag? Don’t the WCF
  tags (below) take name-spacing into
  account?</li>
  <li>Q: Do I HAVE to flatten my object
  hierarchy?</li>
  </ul>
  
  <p><strong>The basic error is…</strong></p>
  
  <blockquote>
    <p>Type
    'wcfFISH.StateManagement.Contractor.New'
    cannot be added to list of known types
    since another type
    'wcfFISH.StateManagement.CostDivision.New'
    with the same data contract name
    'wcfFISH.StateManagement.Contractor:New'
    is already present.</p>
  </blockquote>
  
  <p><strong>The error looks like…</strong></p>
  
  <pre><code>An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
  System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
   contract: http://tempuri.org/:IFishData ----&gt; System.InvalidOperationException: Type 'wcfFISH.StateManagement.Contractor.New' cannot be added to list of known types since another type 'wcfFISH.StateManagement.CostDivision.New' with the same data contract name 'wcfFISH.StateManagement.Contractor:New' is already present.
     at System.Runtime.Serialization.DataContract.CheckAndAdd(Type type, Dictionary`2 typesChecked, Dictionary`2&amp; nameToDataContractTable)
     at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type, Dictionary`2 typesChecked, Dictionary`2&amp; knownDataContracts)
     at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type)
     at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.get_KnownDataContracts()
     at System.Runtime.Serialization.ClassDataContract.get_KnownDataContracts()
     at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
     at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
     at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
     at System.Runtime.Serialization.DataContractSet.Add(XmlQualifiedName name, DataContract dataContract)
     at System.Runtime.Serialization.DataContractSet.Add(Type type)
     at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
     at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType&amp; xsdType)
     at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
     at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
     at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
     at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
     at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
     --- End of inner ExceptionDetail stack trace ---
     at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
     at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
     at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
     at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
     at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
     at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
     at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
     at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
     at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
     at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message&amp; replyMessage)
     at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
     at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
     at SyncInvokeGet(Object , Object[] , Object[] )
     at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
     at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
     at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
  </code></pre>
  
  <p><strong>The service code looks like…</strong></p>
  
  <pre><code>namespace wcfFISH.StateManagement
  {
      [DataContract(Namespace = "wcfFISH.StateManagement")]
      [KnownType(typeof(wcfFISH.StateManagement.C1FE.Approved))]
      [KnownType(typeof(wcfFISH.StateManagement.C1FE.Denied))]
      [KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
      [KnownType(typeof(wcfFISH.StateManagement.C1FE.Pending))]
      [KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
      [KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
      [KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
      [KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]
      [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Closed))]
      [KnownType(typeof(wcfFISH.StateManagement.ReportingPeriod.Open))]
      public abstract class ObjectState
      {
          #region CONSTRUCTORS
  
          protected ObjectState()
          { }
  
          #endregion
  
          #region PROPERTIES &amp; FIELDS
  
          #endregion
  
          #region EVENTS
          #endregion
  
          #region METHODS    
          #endregion
      }
  }
  </code></pre>
  
  <p><strong>Side note…</strong></p>
  
  <p>Another reason I am confused is the original declarations listed below NEVER caused an issue...and it was working fine!  This only happened when I ADDED CostDivision!</p>
  
  <pre><code>[KnownType(typeof(wcfFISH.StateManagement.C1FE.New))]
  [KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
  </code></pre>
  
  <p><strong>In case I’m not being clear…</strong></p>
  
  <p>My understanding is WCF should understand the difference between the following classes:</p>
  
  <pre><code>[KnownType(typeof(wcfFISH.StateManagement.Contractor.Authenticated))]
  [KnownType(typeof(wcfFISH.StateManagement.Contractor.New))]
  [KnownType(typeof(wcfFISH.StateManagement.CostDivision.Authenticated))]
  [KnownType(typeof(wcfFISH.StateManagement.CostDivision.New))]
  </code></pre>
  
  <p><strong>If it does NOT understand…</strong></p>
  
  <p>I guess my only option seems to be to flatten the hierarchy FOR EVERYTHING to:</p>
  
  <pre><code>wcfFISH.StateManagement.Authenticated
  wcfFISH.StateManagement.New
  </code></pre>
  
  <p>…is this my only option? (hope not!)</p>