Skip to content

Microsoft.XmlSerializer.Generator fails with BadImageFormatException if used for a .NET Framework project using MEF #40010

Closed

Description

@AStefanits

Description

We are currently in the process to migrate our projects to use the dotnet compiler instead of MSBuild. For that we have exchanged the old csproj format with the Microsoft.NET.Sdk. But still targeting the .NET Framework 4.6.1.

In order to do that we have also exchanged the old sgen with the Microsoft.XmlSerializer.Generator nuget package.
While running the dotnet build we are facing the issue that the Microsoft.XmlSerializer.Generator fails with "
SGEN : warning SGEN1: Could not load file or assembly 'System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)"

After digging into the issue and getting the source from the master branch I could find the issue.
When the Sgen.cs class checks if the ObsoleteAttribute is used, by using the type.GetCustomAttributes, it tries to load the types. The FileNotFoundException, if a custom attribute is used but defined in another assembly, is handled in the way that the exception is logged and the type is ignored. But the use case the attribute is defined in the .NET Framework itself a ArgumentException containing the BadImageFormatException is thrown.

I tried to create a small change for testing purpose which handles the ArgumentException the same way as the FileNotFoundException.
After the change the XmlSerializer assembly could be generated.

A small project showing the issue is added to my GitHub repo: https://github.com/AStefanits/XmlSerializerGeneratorIssue
Build execution by command line (dotnet build \source\repos\ClassLibrary1\ClassLibrary1\ClassLibrary1.csproj)

Configuration

Project format: Microsoft.NET.Sdk
.NET Framework 4.6.1 (also tested with 4.7.2 and 4.8)
Windows 10 Enterprise 1909 x64
Visual Studio 2019
Microsoft (R) Build Engine version 16.7.0-preview-20360-03+188921e2f for .NET (also tested with different Versions)

Regression?

Tested with Microsoft.XmlSerializer.Generator

  • 1.0.0
  • 2.0.0
  • 2.1.0
  • 2.2.1
  • 5.0.0-preview.7.20364.11

Other information

Exception message and stack trace:
A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.

 at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
 at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
 at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
 at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
 at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
 at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
 at Microsoft.XmlSerializer.Generator.Sgen.GenerateFile(List`1 typeNames, String assemblyName, Boolean proxyOnly, Boolean silent, Boolean warnings, Boolean force, String outputDirectory, Boolean parsableerrors) in \source\repos\runtime\src\libraries\Microsoft.XmlSerializer.Generator\src\Sgen.cs:line 262

Added to Method GenerateFile in Sgen.cs line 289 for testing purpose:

            catch (ArgumentException e)
            {
                if (warnings)
                {
                    Console.Out.WriteLine(FormatMessage(parsableerrors, true,
                        SR.Format(SR.InfoIgnoreType, type.FullName)));
                    WriteWarning(e, parsableerrors);
                }

                continue;
            }

Activity

  1. added
    untriaged
    on Jul 28, 2020
  2. changed the title [-]Microsoft.XmlSerializer.Generator fails with BadImageFormatException if used for a .NET Framwork project using MEF[/-] [+]Microsoft.XmlSerializer.Generator fails with BadImageFormatException if used for a .NET Framework project using MEF[/+] on Jul 28, 2020
  3. ericstj commented on Jul 31, 2020

    @ericstj
    Member

    I believe this is effectively a duplicate of #630 or at least should be solved by that. @mconnew

  4. mconnew commented on Jul 31, 2020

    @mconnew
    Member

    Different problem but possibly solved by the same fix. I'm not sure what the behavior is with the metadata reflection implementation when asked to load a reference assembly. If it still throws, then that won't solve this issue.

  5. ericstj commented on Jul 31, 2020

    @ericstj
    Member

    It does not throw. It would fix this issue.

  6. removed
    untriaged
    on Aug 6, 2020
  7. HongGit commented on Aug 6, 2020

    @HongGit
    Contributor

    You’re hitting this bug because XmlSerializerGenerator is doing runtime-reflection on reference assemblies. You might be able to workaround it by using this hack which is shared for others hitting this problem on other build tools which load build assemblies for execution: dotnet/sdk#1522 (comment).

    The fix for this is quite complex, we would not be able to get to in near term.

  8. added this to the Future milestone on Aug 6, 2020
  9. dotnet-policy-service commented on Feb 5, 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.

  10. added
    backlog-cleanup-candidate
    on Feb 5, 2025
  11. dotnet-policy-service commented on Feb 20, 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.

  12. removed this from the Future milestone on Feb 20, 2025
  13. locked and limited conversation to collaborators on Mar 22, 2025
  14. removed
    backlog-cleanup-candidate
    on Mar 22, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions