2月8日
CRM 4.0 Tutorial: Boost performance with Pre-generated XmlSerializers
Inspired by the article from the MS CRM Team Blog (Boost performance with Pre-generated XmlSerializers) I’ve tested this method with the standard crm web service (no use of Microsoft.Crm.SdkTypeProxy.dll). The instantiation of the crm service object takes about 13 seconds on my CRM 4.0 VPC. After some search on google I found a detailed description on how I generate the XmlSerializers dll (Long time to initialize new VimService object).
Tutorial configuration:
- Microsoft Dynamics CRM 4.0 Virtual PC system
- Visual Studio 2008 with SP1
- C# windows application (.NET Framework 3.5)
Here my step-by-step instructions:
1. Create a project with Visual Studio and add a web reference to the CRM web service (right click on the project in the Solution explorer and select “Add Web Reference”. Insert the CRM web service URL, choose a name [in this example “CrmServiceStandard”] and press the button “Add reference”).
If you doesn’t found the “Add Web Reference” menu entry then you have probably selected a .net 3.0 or 3.5 project. In this case select “Add Service Reference”, “Advanced” and then “Add Web Reference”. The other steps are the same as above.
2. In Windows Explorer go to the “Web References” subfolder of your project and select the folder of your created web reference. Copy the file “References.cs” to an new project subfolder called “CrmServicePerformance” and rename it to “CrmServicePerformance.cs”.
3. Open the “Visual Studio 2008 Command Prompt” (from the start menu) and go to project subfolder “CrmServicePerformance”. Try to compile the “CrmServicePerformance.cs” to “CrmServicePerformance.dll” with the following command.
csc /t:library /out:CrmServicePerformance.dll CrmServicePerformance.cs
At this step you will get the error message “The type or namespace name ‘Properties’ does not exist in the namespace …". This error occurs because the generated web service class references to the project properties to get the web service url dynamically.
4. Open the class “CrmServicePerformance.cs” and change the name of the class to “CrmServicePerformance” (this is only needed for this tutorial). Remove the reference to the project properties and add a new parameter “url” in the constructor.
5. Try to compile the “CrmServicePerformance.cs” to “CrmServicePerformance.dll” with the following command again.
csc /t:library /out:CrmServicePerformance.dll CrmServicePerformance.cs
Now you will get a file “CrmServicePerformance.dll”.
6. Use the sgen tool to pre-generate and compile the XmlSerializers with the following command.
sgen /p CrmServicePerformance.dll
Now you will get a file “CrmServicePerformance.XmlSerializers.dll”. This step takes about some seconds (english version of Visual Studio) to nearly one hour (german version of Visual Studio) (very strange …).
7. Open the file “CrmServicePerformance.cs” and comment out all occurrences of “[System.Xml.Serialization.XmlIncludeAttribute”. Better you use a replace tool, because there are more than 1200 occurrences on a standard crm 4.0 system.
8. Add the attribute “[System.Xml.Serialization.XmlSerializerAssemblyAttribute(AssemblyName = "CrmServicePerformance.XmlSerializers")]” to the “CrmServicePerformance” class.
9. Compile the “CrmServicePerformance.cs” to “CrmServicePerformance.dll” with the following command again.
csc /t:library /out:CrmServicePerformance.dll CrmServicePerformance.cs
10. Add a reference to the “CrmServicePerformance.dll” to your project and try the new generated web service class. To instantiate the crm service object you must call the constructor of the crm web service with the valid url.
11. The performance boost is really impressive.
There are a few drawbacks:
- You must recompile the “CrmServicePerformance.dll” if the crm web service is changed (new entities or attributes).
- The size of the “CrmServicePerformance.dll” and the “CrmServicePerformance.XmlSerializers.dll” is a little greater than the standard web service.
Here you can download my sample project.