0

You can use sgen.exe to create serialization assemblies ahead of time rather than have them created on the fly.

But how does it decide what types to make serializers for? I've tried to switch to using sgen, but it's been telling me that it can't find any applicable types. Is there an attribute you have to add? Or will I need to manually specify the types?

RandomEngy
  • 14,931
  • 5
  • 70
  • 113

2 Answers2

1

I'd guess this is because the types weren't public - XML serialisation only works with public types.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • While true, my types were indeed public. I'm now calling sgen from the command line outside of VS and it seems to be working. I'm not sure what caused the error now. – RandomEngy Jul 19 '11 at 04:15
0

The most common reason for VS-triggered sgen (i.e. via Properties->Build->Generate serialization assembly) to not actually produce a serialization assembly is because the default Sgen build task in VS includes the /proxytypes switch by default, and there is no way to turn it off. This switch tells sgen to only generate serializers for Xml Web service proxy types, as documented here. To generate serializers for other types, you can override the default behavior by adding your own custom task as described in Generating an Xml Serialization assembly as part of my build. You can go further and specify only the types you want serialization code generated for via the Types parameter, e.g.:

<PropertyGroup>
<!-- Specific set of types to create serialization assemblies for -->
<SerializationTypes>
    Type1;
    Type2
</SerializationTypes>
</PropertyGroup>
<!-- Custom non-proxy SGEN invocation to create pre-compiled serialization assemblies -->
<Target Name="GenerateNonProxySerializationAssemblies"
DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)" Types="$(SerializationTypes)"
        BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)"
        ShouldGenerateSerializer="true" UseProxyTypes="false"
        KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)"
        DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)">
        <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
    </SGen>
</Target>
Community
  • 1
  • 1
gregsmi
  • 538
  • 6
  • 14