This article would give a brief idea on using sgen.exe tool for generating the XML Serializers for applications using web services. When the XML Serializer is used, the application may suffer a performance load on its start up. This is because it creates assembly helper classes for all the types specified in the assembly. This performance hit on the start up of an application could be reduced by using the sgen.exe, a .Net SDK tool available in the .Net framewowrk 2.0 and above.
Generating the XMLSerializer files right after the build make the helper classes for the types in the assembly ready to use instead of generating them on run time. The Sgen tool can be used for this and hence it reduces the load of generating them dynamically. This can be done in two ways,
- Visual Studio Project settings
- Using Sgen manually
Project settings option
By setting the project properties in the Visual Studio as given in the following image before build. The below given image represents a C# windows application project property settings.
Project -> Properties -> Build -> Generate Serialization Assembly if set to "On" generates the XML Serializer helper classes for the specified types of the assembly. An assembly in the name <File name>.XMLSerializers.dll would be created after the build as a result of using this option. This will be utilized XMLSerializers and hence no need to generate them on runtime. But this will be generated only if the project has reference to the web service proxy types. This is because of the "proxytypes" option of the Sgen tool is set by default when called from Visual Studio.
The parameter options passed to the Sgen can be viewed by just typing the
<Framework Path>\Bin\Sgen /? in the command prompt.
Using Sgen manually
In that case, we can use the Sgen tool manually to create the assembly for the types other than the web services as follows.
<Framework Path>\Bin\Sgen /a:<assembly name>
It would be something like "C:\Program Files\...\SDK\V2.0\Bin\Sgen" /a:"sample.dll"
This would create a file, "sample.XMLSerializers.dll" which can be used in the application when deployed. But we should ensure before doing this post-build task, the "Generate Serialization Assembly" option is set as given in the above figure.
When including them manually generated assembly using Sgen, it should be ensured that the build project should have the "XML Serialization Assemblies" option included in the "Add Project Output Group" option in the Visual Studio for the setup project as given in the above image.
Generating the Serialization assembly can also be done using the Sgen MSBuild task. By this we can avoid manual generation of the serialization assembly using Sgen. After setting the "Generate Serialization Assembly" option to "On" in the Visual Studio, the .csproj file can be modified by setting the Sgen properties especially the property, UseProxyTypes=false.