Skip to content

Microsoft.XmlSerializer.Generator generated serializers are not used in runtime #121440

@skrecc

Description

@skrecc

Hello,

I have a very simple project testing that pre-generated serializers by Microsoft.XmlSerializer.Generator are used during runtime.

Issue:
Pre-generated serializers are not used during runtime

Current behavior:
Dynamically generated System.Xml.Serialization.XmlSerializer from System.Private.Xml assembly is used instead

Test performed for type:

[XmlRoot]
public class XmlRootAttributedClass
{
    public XmlRootAttributedClass()
    {
    }

    public string? XmlContent { get; set; }
}

Serializers assembly XMLSerializerGeneratorTests.XmlSerializers.dll is created correctly in output directory and contains XmlRootAttributedClassSerializer

Output log of testing app:

Loaded: System.Collections.NonGeneric, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Loaded: System.Xml.ReaderWriter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Resolving: XMLSerializerGeneratorTests.XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Loaded: XMLSerializerGeneratorTests.XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Loaded: System.Text.RegularExpressions, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
SGEN Serializer implementation assembly: System.Private.Xml loaded for serialized type name: XMLSerializerGeneratorTests.XmlRootAttributedClass. Serializer type name: System.Xml.Serialization.XmlSerializer
SGEN Location: 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\9.0.10\System.Private.Xml.dll'  IsDynamic: False
SGEN Using runtime-generated (reflection) serializer implementation.
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net9.0-windows</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.10" />
	</ItemGroup>
	<PropertyGroup>
		<SGenVerbose>true</SGenVerbose>
		<OutputType>Exe</OutputType>
	</PropertyGroup>  

</Project>
public static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        AssemblyLoadContext.Default.Resolving += (ctx, name) =>
        {
            Console.WriteLine($"Resolving: {name}");
            return null;
        };

        AppDomain.CurrentDomain.AssemblyLoad += (_, e) =>
        {
            Console.WriteLine($"Loaded: {e.LoadedAssembly.FullName}");
        };

        var serializedTypeName = typeof(XmlRootAttributedClass);
        var serializer = new XmlSerializer(serializedTypeName);

        var serializerType = serializer.GetType();
        var implAsm = serializerType.Assembly;
        Console.WriteLine($"SGEN Serializer implementation assembly: {implAsm.GetName().Name} loaded for serialized type name: {serializedTypeName}. Serializer type name: {serializerType.FullName}");
        Console.WriteLine($"SGEN Location: '{implAsm.Location}'  IsDynamic: {implAsm.IsDynamic}");

        bool usedPreGenerated = implAsm.GetName().Name != null
                                && implAsm.GetName().Name.EndsWith("XmlSerializers")
                                && !string.IsNullOrEmpty(implAsm.Location);

        Console.WriteLine(usedPreGenerated
            ? "SGEN Using pre-generated serializer implementation."
            : "SGEN Using runtime-generated (reflection) serializer implementation.");

        Console.ReadLine();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions