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

My work as a recruited developer changes almost every month. 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s