title: WCF error: Need to exclude all but one of the following types. Only matching types can be valid references
body: <p>I'm getting this error with a WCF service.  This is the error report I get when I use the Microsoft Service Trace Viewer to view the error details:</p>
  
  <p>An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter
  Error: List of referenced types contains more than one type with data contract name 'EchoAddress' in namespace '<a href="http://schemas.datacontract.org/2004/07/WorldVision.NewZealand.CIMS.Common.Entities" rel="nofollow noreferrer">http://schemas.datacontract.org/2004/07/WorldVision.NewZealand.CIMS.Common.Entities</a>'. Need to exclude all but one of the following types. Only matching types can be valid references: 
  "CIMS.CrmService.EchoAddress, App_WebReferences.k4hj-nad, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" (matching)
  "CIMS.AddressService.EchoAddress, App_WebReferences.k4hj-nad, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" (matching)</p>
  
  <p>It reads like there are two separate classes called 'EchoAddress' that I've defined as WCF DataContracts using the [DataContract] attribute, that both exist in the WorldVision.NewZealand.CIMS.Common.Entities namespace.  I've checked and this isn't the case.  The error also says I need to exclude one of the following types:</p>
  
  <p>CIMS.CrmService.EchoAddress
  CIMS.AddressService.EchoAddress</p>
  
  <p>But I only declare the 'EchoAddress' class once, in the WorldVision.NewZealand.CIMS.Common.Entities namespace.  It looks like these are references to the same type (EchoAddress), within both the 'CrmService' and 'AddressService', which are two separate WCF services that I'm referencing as 'service references' from my main project.</p>
  
  <p>This is the type declaration for the EchoAddress class, which is declared in a separate project that then gets referenced by the project that contains the actual WCF services.  My main project then references the service as a service reference:</p>
  
  <pre><code>[DataContract]
  public class EchoAddress
  {
      [DataMember]
      public string AddressType { get; set; }
      [DataMember]
      public Dictionary&lt;string,string&gt; AddressLines { get; set; }
  
      ...
  }
  </code></pre>
  
  <p>I've tried the following:</p>
  
  <ul>
  <li>In VS2010, right-click the WCF service reference > configure service reference > unchecked 'Reuse types in reference assemblies from the Advanced menu of the webservice reference configuration, for both the CrmService and AddressService WCF services.</li>
  <li>Removed web service references, closed down VS2010, opened VS2010 with 'run as adminstrator', re-added web service references.</li>
  <li>Searched entire solution for duplicate references to EchoAddress</li>
  <li>Set [DataContract(IsReference = false)] for all data types, as per:
  <a href="http://blogs.msdn.com/b/distributedservices/archive/2010/02/04/wcf-client-issue-with-reuse-types-from-referenced-assemblies.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/distributedservices/archive/2010/02/04/wcf-client-issue-with-reuse-types-from-referenced-assemblies.aspx</a></li>
  <li>Checked that there are no class members anywhere marked with [DataMember] that are also called EchoAddress' (apparently this error can occur when this type of structure exists - see the above link for details)</li>
  <li>Checked all project references, to ensure there are no 'circular' type references (e.g. project 1 references project 2, project 1 has service reference to service1, project 2 also has service reference to service1.  Have checked for service references, project references and 'standard' dll references that could have caused this.</li>
  <li>Tried updating the service references, rebuilding individual projects, entire solution, every combination you can think of!</li>
  <li>Tried stepping through in the debugger - doesn't give any more info, just fails at the point where I'm trying to write into our CRM via the WCF service, using OrganizationServiceContext.SaveChanges() from the MS Dynamics CRM API
  
  <ul>
  <li>Have read these:</li>
  <li><a href="http://social.msdn.microsoft.com/Forums/en/wcf/thread/6ab067fe-0607-4b6e-8ab7-a5e2db1dd490" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en/wcf/thread/6ab067fe-0607-4b6e-8ab7-a5e2db1dd490</a></li>
  <li><a href="https://stackoverflow.com/questions/1872865/what-does-this-wcf-error-mean-custom-tool-warning-cannot-import-wsdlporttype">What does this WCF error mean: &quot;Custom tool warning: Cannot import wsdl:portType&quot;</a></li>
  <li><a href="http://www.lukepuplett.com/2010/07/note-to-self-don-let-wcf-svcutil-reuse.html" rel="nofollow noreferrer">http://www.lukepuplett.com/2010/07/note-to-self-don-let-wcf-svcutil-reuse.html</a></li>
  <li>and am just about finished reading:</li>
  <li><a href="http://marktinderholt.wordpress.com/2010/02/12/silverlight-wcf-service-reference-weirdness-solved-custom-tool-error-failed-to-generate-code-for-the-service-reference-%E2%80%98servicereference1%E2%80%99-please-check-other-error-and-warnin/" rel="nofollow noreferrer">http://marktinderholt.wordpress.com/2010/02/12/silverlight-wcf-service-reference-weirdness-solved-custom-tool-error-failed-to-generate-code-for-the-service-reference-%E2%80%98servicereference1%E2%80%99-please-check-other-error-and-warnin/</a></li>
  </ul></li>
  </ul>
  
  <p>Any ideas?</p>