How to: Improve the Startup Time of WCF Client Applications using the XmlSerializer

Services and client applications that use data types that are serializable using the XmlSerializer generate and compile serialization code for those data types at runtime, which can result in slow start-up performance.

Note

Pre-generated serialization code can only be used in client applications and not in services.

The ServiceModel Metadata Utility Tool (Svcutil.exe) can improve start-up performance for these applications by generating the necessary serialization code from the compiled assemblies for the application. Svcutil.exe generates serialization code for all data types used in service contracts in the compiled application assembly that can be serialized using the XmlSerializer. Service and operation contracts that use the XmlSerializer are marked with the XmlSerializerFormatAttribute.

To generate XmlSerializer serialization code

  1. Compile your service or client code into one or more assemblies.

  2. Open an SDK command prompt.

  3. At the command prompt, launch the Svcutil.exe tool using the following format.

    svcutil.exe /t:xmlSerializer  <assemblyPath>*
    

    The assemblyPath argument specifies the path to an assembly that contains service contract types. Svcutil.exe generates serialization code for all data types used in service contracts in the compiled application assembly that can be serialized using the XmlSerializer.

    Svcutil.exe can only generate C# serialization code. One source code file is generated for each input assembly. You cannot use the /language switch to change the language of the generated code.

    To specify the path to dependent assemblies, use the /reference option.

  4. Make the generated serialization code available to your application by using one of the following options:

    1. Compile the generated serialization code into a separate assembly with the name [original assembly].XmlSerializers.dll (for example, MyApp.XmlSerializers.dll). Your application must be able to load the assembly, which must be signed with the same key as the original assembly. If you recompile the original assembly, you must regenerate the serialization assembly.

    2. Compile the generated serialization code into a separate assembly and use the XmlSerializerAssemblyAttribute on the service contract that uses the XmlSerializerFormatAttribute. Set the AssemblyName or CodeBase properties to point to the compiled serialization assembly.

    3. Compile the generated serialization code into your application assembly and add the XmlSerializerAssemblyAttribute to the service contract that uses the XmlSerializerFormatAttribute. Do not set the AssemblyName or CodeBase properties. The default serialization assembly is assumed to be the current assembly.

Example

The following command generates serialization types for XmlSerializer types that any service contracts in the assembly use.

svcutil /t:xmlserializer myContractLibrary.exe

See Also

Other Resources

ServiceModel Metadata Utility Tool (Svcutil.exe)


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21