title: Why can't I use WCF DataContract and ISerializable on the same class?
body: <p>I have a class that I need to be able to serialize to a SQLServer session variable and be available over a WCF Service. I have declared it as follows</p>
  
  <pre><code>namespace MyNM
  {
  [Serializable] 
  [DataContract(Name = "Foo", Namespace = "http://www.mydomain.co.uk")]
  
  public class Foo : IEntity, ISafeCopy&lt;Foo&gt;
  {
      [DataMember(Order = 0)] 
      public virtual Guid Id { get; set; }
  
      [DataMember(Order = 1)] 
      public virtual string a { get; set; }
  
      DataMember(Order = 2)]
      public virtual Bar c { get; set; }
  
      /* ISafeCopy implementation */
  }
  
  
  [Serializable]
  [DataContract(Name = "Bar ", Namespace = "http://www.mydomain.co.uk")]
  public class Bar : IEntity, ISafeCopy&lt;Bar&gt;
  {
      #region Implementation of IEntity
  
      DataMember(Order = 0)]
      public virtual Guid Id { get; set; }
      [DataMember(Order = 1)]
      public virtual Baz y { get; set; }
  
      #endregion
  
      /* ISafeCopy implementation*/
  }
  
  
  [Serializable]
  [DataContract]
  public enum Baz
  {
      [EnumMember(Value = "one")]
      one,
      [EnumMember(Value = "two")]
      two,
      [EnumMember(Value = "three")]
      three
  }
  </code></pre>
  
  <p>But when I try and call this service, I get the following error in the trace log.</p>
  
  <blockquote>
    <p>"System.Runtime.Serialization.InvalidDataContractException:
    Type
    'BarProxybcb100e8617f40ceaa832fe4bb94533c'
    cannot be ISerializable and have
    DataContractAttribute attribute."</p>
  </blockquote>
  
  <p>If I take out the Serializable attribute, the WCF service works, but when the object can't be serialized to session. If I remove the DataContract attribute from class Bar, the WCF service fails saying </p>
  
  <blockquote>
    <p>Type
    'BarProxy3bb05a31167f4ba492909ec941a54533'
    with data contract name
    'BarProxy3bb05a31167f4ba492909ec941a54533:<a href="http://schemas.datacontract.org/2004/07/" rel="noreferrer">http://schemas.datacontract.org/2004/07/</a>'
    is not expected. Add any types not
    known statically to the list of known
    types - for example, by using the
    KnownTypeAttribute attribute or by
    adding them to the list of known types
    passed to DataContractSerializer</p>
  </blockquote>
  
  <p>I've tried adding a KnownType attribute to the foo class </p>
  
  <pre><code>[KnownType(typeof(Bar))]
  </code></pre>
  
  <p>But I still get the same error.</p>
  
  <p>Can anyone help me out with this? </p>
  
  <p>Many thanks </p>
  
  <p>Dave </p>