title: Workflow SerializationException because of complex object
body: <p>I got the following exception:</p>
  
  <blockquote>
    <p>System.Workflow.Runtime.Hosting.PersistenceException: Type ‘Microsoft.SharePoint.SPWeb’ in Assembly ‘Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ is not marked as serializable. —> System.Runtime.Serialization.SerializationException: Type ‘Microsoft.SharePoint.SPWeb’ in Assembly ‘Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ is not marked as serializable</p>
  </blockquote>
  
  <p>The error came from here:</p>
  
  <pre><code>public sealed partial class MyWorkflow : StateMachineWorkflowActivity
  {
      public SPWorkflowActiviationProperties workflowProperties = new SPWorkflowActivationProperties();
      private SPWeb spWebtemp;
  
      private SPWeb spWeb
      {
          get { return spWebtemp ?? (spWebtemp = workflowProperties.Web); }
      }
  
      ...
  </code></pre>
  
  <p>There are two blog posts I found:</p>
  
  <ul>
  <li><a href="http://pradeepkvd.wordpress.com/2010/01/14/microsoft-sharepoint-spuser-in-assembly-microsoft-sharepoint-version12-0-0-0-cultureneutral-publickeytoken71e9bce111e9429c-is-not-marked-as-serializable/" rel="nofollow">... is not marked as serializable</a></li>
  <li><a href="http://www.sharepointassist.com/2009/12/01/serialization-issue-with-windows-workflow-foundation-and-sharepoint-workflow/" rel="nofollow">Serialization Issue with Windows Workflow Foundation and Sharepoint Workflow</a></li>
  </ul>
  
  <p>There is one solution to be found to this problem: <em>Not have complex member objects as global variables</em>, but as a local variables - i.e. declare SPWeb locally (workflowProperties.Web) instead of on a global level.</p>
  
  <p>So I would have to redeclare spWeb in every method I am using - which I deem rather ugly.</p>
  
  <p>What I also tried is this:</p>
  
  <pre><code>...
  [NonSerialized]
  private SPWeb spWebtemp;
  
  private SPWeb spWeb
  {
      get { return spWebtemp ?? (spWebtemp = workflowProperties.Web); }
  }
  ...
  </code></pre>
  
  <p>==> no more serialization exception!</p>
  
  <p><strong>Are there any negative implications when using the <code>NonSerialized</code> attribute on this field?</strong><br>
  Or in other words - what are the implications?</p>