SGEN during build and deploy

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>

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.