title: XmlException in WCF deserialization: "Name cannot begin with '<'" - in automatic property backing fields
body: <p>I have started experiencing errors in WCF deserialization today - in code which has been unchanged and working for months. </p>
  
  <p>The issue is that I am getting runtime <code>XmlException</code>s saying 'Name cannot begin with the '&lt;' character'. I have debugged into the .NET source, and it seems the error is in deserializing return objects from our WCF service calls. These objects are defined using automatic properties, and it seems the backing fields are given names like <code>&lt;MyProperty&gt;k_BackingField</code>, which is where the XmlException is coming from.</p>
  
  <p>I've seen a couple of other references online where the solution people accept is "I changed my code to not use automatic properties", which isn't really acceptable to me, as I would have 100s of objects to change, (with 1000s of properties amongst them). Also, this same code was working fine when I ran it last week, and doesn't seem to affect all serialized DTOs, only some.</p>
  
  <p>To make it even more frustrating, it seems mildly intermittent. On occasion this morning, there has been no exception thrown...!</p>
  
  <p>Questions;</p>
  
  <ol>
  <li>Why has this problem suddenly appeared in unchanged code and unchanged framework source?</li>
  <li>How do I fix this without modifying all the DTOs to use fully implemented properties?</li>
  </ol>
  
  <hr>
  
  <p><strong>UPDATE:</strong> After a day or so of working fine, this issue has reappeared - no reason I can find why it would work/not work/work again, but here we are.</p>
  
  <p>I have tracked the problem down further to be related to some code I have on my ServiceContracts using the <code>ServiceKnownType</code> attribute, which is used to define known types for serialization. It seems that although the types being reported with errors are not even part of the service call I am making at the time, that this error is occurring on types which are part of this known types 'publishing' behaviour. </p>
  
  <p>The problem occurs when I use some proxy creation code to apply some service behaviours;</p>
  
  <pre><code>IOperationBehavior innerBehavior = new PreserveReferencesOperationBehavior(
      description, this.preserveReferences, this.maxItemsInObjectGraph);
  innerBehavior.ApplyClientBehavior(description, proxy);
  </code></pre>
  
  <p>I cannot debug the <code>ApplyClientBehavior</code> code as it is part of <code>System.ServiceModel</code> (or can I?), but something in that method is trying to validate all types I have published using my <code>ServiceKnownType</code> attribute, and breaking on some of them with this <code>XmlException</code>. I have NO IDEA why some of the types are failing - and only for <em>some</em> of their properties.</p>
  
  <p>This is an example of the types which are getting errors reported against them;</p>
  
  <pre><code>[Serializable]
  public class MyDataObject
  {
      public ActivitySession(string id)
      {
          this.Id = id;
          this.IsOpen = true;
      }
  
      public string Id { get; set; }
  
      public bool IsValid { get; set; }
  }
  </code></pre>
  
  <p>The exception reported an error against <code>Id</code> -> <code>&lt;Id&gt;k_BackingField cannot start with '&lt;'</code></p>
  
  <p>So nothing controversial in that class, and no inheritance to consider. It's not even part of a service contract, only it was previously published as a known type for serialization.</p>
  
  <p>This is getting quite esoteric now, so I'm not expecting an answer, but just updating where the problem is at.</p>