This documentation is archived and is not being maintained.

Using Pre-generated XmlSerializers to Increase Performance

Forefront
banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Creating an XmlSerializer instance has a well-known performance cost because the .NET Framework runtime generates a temporary helper assembly that requires a compiler call. Additionally, the assembly cannot be unloaded without unloading the hosting application domain. This causes high memory usage when several XmlSerializer objects are constructed.

Sometimes this assembly-generating process happens in a more indirect manner like the instantiation of a Web service proxy class. For example, creating a new instance of CrmService requires an XmlSerializer for the type CrmService, which forces the .NET run-time to generate code, compile, and load the assembly. The following three methods can improve performance and minimize memory usage when you create XmlSerializer objects or instantiate Web service proxy classes.

Use the Pre-generated XmlSerializer for the CrmService Type

If you use a CrmService instance to interact with the Microsoft Dynamics CRM server and your project references the Microsoft.Crm.SdkTypeProxy.dll assembly, you can add another reference to the Microsoft.Crm.SdkTypeProxy.XmlSerializers.dll assembly. Because you can use the pre-generated serializer, the .NET runtime will not have to generate a temporary assembly. This method has been shown to result in a noticeable 4 to 6 second performance gain for the CrmService constructor.

Some things to consider are as follows:

  • The processor architecture and version of the Microsoft.Crm.SdkTypeProxy.XmlSerializers.dll assembly must match that of the Microsoft.Crm.SdkTypeProxy.dll assembly to use the pre-generated serializers.
  • If your code is deployed to the Microsoft Dynamics CRM server, the Microsoft.Crm.SdkTypeProxy.XmlSerializers.dll assembly is already in the GAC, so you will automatically see the benefit of pre-generated serializers for the CrmService type.
  • Unless you deploy the code to the Microsoft Dynamics CRM server, this gain does not apply if you have a Web reference to CrmService instead of an assembly reference to Microsoft.Crm.SdkTypeProxy.dll.

The serializer assemblies are available in the SDK\bin folder of the SDK download. Alternatively you can copy the serializer assembly from the GAC on your Microsoft Dynamics CRM server to your local project directory by executing the following command in a Command Prompt Window:

copy c:\Windows\assembly\GAC_MSIL\Microsoft.Crm.SdkTypeProxy.XmlSerializers\4.0.0.0__31bf3856ad364e35\Microsoft.Crm.Sdk.dll <destination folder>

Use the Simple XmlSerializer Constructors

When you create an XmlSerializer instance, be very conscientious about the constructor you use. If you use the XmlSerializer(Type) and XmlSerializer(Type, String) constructors, your serializer assemblies will be reused until the application domain is unloaded. However, they still are generated the first time that they are needed. If you use any other XmlSerializer constructors, a new generated assembly is loaded every time that you instantiate an XmlSerializer. This can cause memory and performance losses.

Generate Your Own Serializers

If you have created types that have to be serialized using XmlSerializer, then, when possible, you should generate the serializer assemblies for these types at build time by using tools such as sgen or xgen. This way you avoid having the .NET run time generate temporary assemblies dynamically.

For example, if you created a type named MyType in the assembly named MyAssembly.dll and you know this type will be serialized by an XmlSerializer, you should run the following command in a Command Prompt Window:

Sgen.exe /t:MyType /a:MyAssembly.dll

This command generates the serializer assembly MyAssembly.XmlSerializers.dll, which should be added as a reference to the current project or to the GAC.

For more information about the different XmlSerializer constructors, see the MSDN Library topic XmlSerializer Class.

For more information about the Sgen tool, see the MSDN Library topic XML Serializer Generator Tool (Sgen.exe).


© 2010 Microsoft Corporation. All rights reserved.


Show: