With sgen you can generate a XML Serializer assembly to ship with your application. This way the .NET framework can load the assembly in stead of generating a temporary type every time you need the XML Serializer.
To generate the XmlSerializers assembly on every build include this in your build/csproj file. This will generate a new assembly in your output folder with the name ASSEMBLY.XmlSerializers.dll. For all types in itemgroup SgenTypes the xmlserializer is included in the assembly.
<Target Name="AfterBuild"> <ItemGroup> <SgenTypes Include="MY.NAMESPACE.TYPE" /> </ItemGroup> <Sgen ShouldGenerateSerializer="true" UseProxyTypes="false" BuildAssemblyName="ASSEMBLY.dll" BuildAssemblyPath="$(TargetDir)" Types="@(SgenTypes)" /> </Target>
When you want the same for website projects but only for deployment, use the code below. It extends the CopyAllFilesToSingleFolderxxxx target to generate the XmlSerializers assembly and to include it in the deploy / package folder.
<PropertyGroup> <CopyAllFilesToSingleFolderForPackageDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForPackageDependsOn); </CopyAllFilesToSingleFolderForPackageDependsOn> <CopyAllFilesToSingleFolderForMsDeployDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForPackageDependsOn); </CopyAllFilesToSingleFolderForMsDeployDependsOn> </PropertyGroup> <Target Name="CustomCollectFiles"> <!-- Generate XmlSerializers --> <ItemGroup> <SgenTypes Include="MY.NAMESPACE.TYPE" /> </ItemGroup> <Sgen ShouldGenerateSerializer="true" UseProxyTypes="false" BuildAssemblyName="ASSEMBLY.dll" BuildAssemblyPath="$(TargetDir)" Types="@(SgenTypes)" /> <!-- XmlSerializers to publish directory --> <ItemGroup> <_CustomFiles Include="$(TargetDir)*.XmlSerializers.dll"> <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </_CustomFiles> <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)"> <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </FilesForPackagingFromProject> </ItemGroup> <ItemGroup> <_CustomFiles2 Include="Deployment\*"> <DestinationRelativePath>..\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </_CustomFiles2> <FilesForPackagingFromProject Include="%(_CustomFiles2.Identity)"> <DestinationRelativePath>..\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </FilesForPackagingFromProject> </ItemGroup> </Target>