title: Can VS.NET 2010/MSBUILD produce XmlSerializers for .NET 3.5 SP1?
body: <p>I just upgraded a VS 2008 solution containing WinForms, general use libraries, and a web app to VS 2010, but all projects still target .NET 3.5 SP 1. I use <a href="https://stackoverflow.com/questions/134224/generating-an-xml-serialization-assembly-as-part-of-my-build/204993#204993">this technique</a> to generate XmlSerializers for my general use libraries. The WinForms app runs fine. When my web app tries to run using these libraries that reference the same XmlSerializers, it throws the following:</p>
  
  <blockquote>
    <p>Server Error in '/WebSubscribers'
    Application. Could not load file or
    assembly
    'Ceoimage.Basecamp.XmlSerializers' or
    one of its dependencies. This assembly
    is built by a runtime newer than the
    currently loaded runtime and cannot be
    loaded. Description: An unhandled
    exception occurred during the
    execution of the current web request.
    Please review the stack trace for more
    information about the error and where
    it originated in the code.</p>
    
    <p>Exception Details: System.BadImageFormatException: Could not load file or assembly 'Ceoimage.Basecamp.XmlSerializers' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.</p>
  </blockquote>
  
  <p>I have looked at the XmlSerializer's references using <strong>.NET Reflector</strong> and see it references both the 2.0 and 4.0 versions of <code>mscorlib</code> as well as the 3.5 and 4.0 versions of <code>System.Data.Linq</code>. Strangely, it only uses the 4.0 version of <code>System.Xml</code>. That is probably my problem right there.</p>
  
  <p>How can I get the web app to run using these XmlSerializers? When I simply delete those XmlSerializers, the web app runs fine. This is an option, but how can I force MSBUILD to create serializers for a specific version of the CLR?</p>
  
  <p>Here is the MSBuild task I add to project files that forces the creation of the XmlSerializers:</p>
  
  <pre><code>&lt;Target Name="AfterBuild" DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource" Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)" Outputs="$(OutputPath)$(_SGenDllName)"&gt;
   &lt;Delete Files="$(TargetDir)$(TargetName).XmlSerializers.dll" ContinueOnError="true" /&gt;
   &lt;SGen BuildAssemblyName="$(TargetFileName)" BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)" ShouldGenerateSerializer="true" UseProxyTypes="false" KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)" DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)"&gt;
    &lt;Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" /&gt;
   &lt;/SGen&gt;
  &lt;/Target&gt;
  </code></pre>