Skip to content

XmlSerializer loading generated assembly is not linker safe #37754

Closed

Description

@eerhardt

If an application uses the Microsoft.XmlSerializer.Generator to generate an assembly for xml serialization/deserialization and the application gets trimmed by the ILLinker, the linker won't be able to recognize that it should keep the code in the generated assembly.

This is because we are dynamically loading the assembly:

if (!string.IsNullOrEmpty(serializerPath))
{
serializer = Assembly.LoadFile(serializerPath);
}

And then using reflection to grab a type out of it:

Type contractType = GetTypeFromAssembly(serializer, "XmlSerializerContract");

internal static Type GetTypeFromAssembly(Assembly assembly, string typeName)
{
typeName = GeneratedAssemblyNamespace + "." + typeName;
Type type = assembly.GetType(typeName);
if (type == null)
throw new InvalidOperationException(SR.Format(SR.XmlMissingType, typeName, assembly.FullName));
return type;
}

The linker won't be able to know to analyze the generated assembly because it is not referenced by the application. And if/when it did analyze the generated assembly, it wouldn't know to keep the generated XmlSerializerContract type.

Activity

  1. added
    untriaged
    on Jun 11, 2020
  2. added
    linkable-framework
    on Jun 11, 2020
  3. MichalStrehovsky commented on Jun 12, 2020

    @MichalStrehovsky
    Member

    Other serializers that offer pregeneration solve this by asking users to explicitly initialize the generated code - this adds a static reference to it that linker can analyze: e.g. this is how Utf8json does this: https://github.com/neuecc/Utf8Json/#pre-code-generationunityxamarin-supports

    We should ideally add something like that (if the generated code is otherwise supportable with linker - not sure about that).

  4. added this to the Future milestone on Jul 8, 2020
  5. removed
    untriaged
    on Jul 8, 2020
  6. dotnet-policy-service commented on Jan 7, 2025

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  7. added
    backlog-cleanup-candidate
    on Jan 7, 2025
  8. dotnet-policy-service commented on Jan 21, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  9. removed this from the Future milestone on Jan 21, 2025
  10. locked and limited conversation to collaborators on Feb 21, 2025
  11. removed
    backlog-cleanup-candidate
    on Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Xmllinkable-framework

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions