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
Compile your service or client code into one or more assemblies.

Open an SDK command prompt.

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.

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

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.

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.

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