sgen.exe fails during build

If you are having this problem while building your VS.NET project in Release mode here is the solution:

Go to the project properties and click on the Build tab and set the value of the "Generate Serialization Assembly" dropdown to "Off".

Sgen.exe is "The XML Serializer Generator creates an XML serialization assembly for types in a specified assembly in order to improve the startup performance of a XmlSerializer when it serializes or deserializes objects of the specified types." (MSDN)


see msdn for the options to sgen.exe [you have the command line, you can play with it manually... delete your .XmlSerializers.dll or use /force though]

Today I also ran across how to more manually specify the sgen options. I wanted this to not use the /proxy switch, but it appears it can let you specify the output directory. I don't know enough about msbuild to make it awesome, but this should get you started [open your .csproj/.vbproj in your non-visual studio editor of choice, look at the bottom and you should be able to figure out how/where this goes]

[the below code has had UseProxyTypes set to true for your convenience]

<Target Name="GenerateSerializationAssembliesForAllTypes"
  DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
  Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
  Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)"
      BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)"
      ShouldGenerateSerializer="true" UseProxyTypes="true"
      KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)"
      DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)">
        <Output TaskParameter="SerializationAssembly"
          ItemName="SerializationAssembly" />
     </SGen>
</Target>
<!-- <Target Name="BeforeBuild">
</Target> -->
<Target Name="AfterBuild"
  DependsOnTargets="GenerateSerializationAssembliesForAllTypes">
</Target>