Search code examples
.netmstest.net-standard.net-framework-version

XmlSerializer exception: You must add a reference to assembly netstandard


I'm working on a solution that has a mixture of the following target frameworks:

  • .Net Framework 4.7.2
  • .Net 6.0
  • .Net Standard 2.0

(Note: None of our projects target a Framework version lower than 4.7.2).

I have been gradually upgrading some library projects from 4.7.2 to Standard 2.0, as well as some frontend projects from 4.7.2 to 6.0. After having upgraded several projects already, some of our unit tests are failing, all caused by a singular line of code:

var serializer = new XmlSerializer(typeof(BusinessClass));

The XmlSerializer class belongs to the System.Xml.Serialization namespace. The BusinessClass class exists within a .Net Standard 2.0 library project.

The project running these tests is a .Net Framework 4.7.2 MSTest project.

The full exception is:

System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
error CS0012: The type 'System.Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

It causes multiple test failures but all of them are caused by this single reference to XmlSerializer.

I have taken the following steps to try and solve the issue, to no avail:

  • I have tried referencing the NETStandard.Library nuget package in the test project.

  • Under the properties of the netstandard reference, I have set Copy Local to True, and have confirmed that the netstandard.dll appears in the bin folder.

  • I have added the following to the csproj file for the test project:

    <Reference Include="netstandard">
        <Private>True</Private>
    </Reference>
    
  • I have added a bindingRedirect in the app.config like so:

    <dependentAssembly>
      <assemblyIdentity name="netstandard" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.3.0" />
    </dependentAssembly>
    
  • I have reversed the bindingRedirect to force a downgrade to 2.0.0.0.

  • I have referenced the System.Xml.XmlSerializer nuget package to the MSTest project.

  • I have referenced the System.Xml.XmlSerializer nuget package to the library containing the BusinessClass.

None of the above steps have fixed the issue. I am starting to feel as if the relation to netstandard is symptomatic, rather than the cause.

Unfortunately I am out of ideas. What other approaches could I take to debug the issue further?


Solution

  • So it turns out that if you use the XmlSerializer in a .Net Standard or .Net Core project, you'll get this exception. However the message is very misleading, and the fix has nothing to do with adding a reference to netstandard at all.

    The fix was actually remarkably simple: I added a Nuget reference to Microsoft.XmlSerializer.Generator and it suddenly worked. This package is required at runtime for using the XmlSerializer in a .Net Standard or .Net Core context.

    Microsoft article regarding this: https://learn.microsoft.com/en-gb/dotnet/core/additional-tools/xml-serializer-generator