XML Serialization Assembly Generation With Visual Studio

The built-in option to “generate XML serialization assembly” in Visual Studio does not work. It only works for SOAP web proxies and this seems to have been hard-coded by Microsoft and they are not changing it. The open Microsoft Connect bug reports have been outstanding since Visual Studio 2008 was released and still exists in 2010.

Here is how to workaround the problem. Other people just call SGEN in the project post-build commands, but that does not flag the result as project output. A better way is to edit the project file in Notepad and add a call to the SGen MSBuild task in the “AfterBuild” task, as follows:-

<!—Call XML serialization assembly generation task after build because built-in Visual Studio option does not work –>
<Target Name="AfterBuild" DependsOnTargets="GenSerializationAssembly" />
<!– XML Serialization Assembly Generation –>
<Target Name="GenSerializationAssembly"
        DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
        Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
        Outputs="$(OutDir)$(_SGenDllName)">
  <SGen BuildAssemblyName="$(TargetFileName)"
        BuildAssemblyPath="$(OutDir)"
        References="@(ReferencePath)"
        ShouldGenerateSerializer="true"
        UseProxyTypes="false"
        KeyContainer="$(KeyContainerName)"
        KeyFile="$(KeyOriginatorFile)"
        DelaySign="$(DelaySign)"
        ToolPath="$(SGenToolPath)">
    <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
  </SGen>
</Target>

Leave a Reply

Your email address will not be published. Required fields are marked *