Microsoft.XmlSerializer.Generator broken on .NET 10.0 RC
Description
Using Microsoft.XmlSerializer.Generator as documented on https://learn.microsoft.com/en-us/dotnet/core/additional-tools/xml-serializer-generator does not work on .NET 10.0 RC due to a System.Runtime assembly loading issue. Attempting to use the serializer generator as documented causes the following output during build:
sgen succeeded with 3 warning(s) (0,8s) → bin\Debug\net10.0\sgen.dll
SGEN : warning SGEN1: Could not load file or assembly 'C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\10.0.0-rc.1.25451.107\ref\net10.0\System.Runtime.dll'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
SGEN : warning SGEN1: Could not load file or assembly 'C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\10.0.0-rc.1.25451.107\ref\net10.0\System.Runtime.dll'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
C:\Users\Markus\.nuget\packages\microsoft.xmlserializer.generator\10.0.0-rc.1.25451.107\build\Microsoft.XmlSerializer.Generator.targets(52,5): warning SGEN: Failed to generate the serializer for sgen.dll. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again.
We depend on the XML serializer generator in our products, so this is a blocking issue for us in order to release a .NET 10.0 version of our products.
Note that these errors are emitted when using Microsoft.XmlSerializer.Generator package and tool version 10.0.0-rc.1.25451.107.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.XmlSerializer.Generator" Version="10.0.0-rc.1.25451.107" />
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="10.0.0-rc.1.25451.107" />
</ItemGroup>
</Project>
Reproduction Steps
Follow the minimal example shown at https://learn.microsoft.com/en-us/dotnet/core/additional-tools/xml-serializer-generator using .NET 10.0 RC.
Expected behavior
The serializer should be generated.
Actual behavior
The serializer fails to be generated because of a System.Runtime assembly loading problem.
Regression?
This worked on .NET 6.0 and .NET 8.0.
Known Workarounds
Disabling the use of the serializer generator, but this causes an unacceptable performance hit in our product.
Configuration
.NET 10.0.0-rc.1.25451.107 Windows 11 24H2 (OS build 26100.6584) x64
Same problem occurs on Linux.
Other information
Issue previously reported on https://developercommunity.visualstudio.com/t/MicrosoftXmlSerializerGenerator-broken/10980787 ; was told to report it here.
Issue still present on .NET 10.0.100-rc.2.25502.107, with package version 10.0.0-rc.2.25502.107.
The problem seems to be that Microsoft.XmlSerializer.Generator.targets uses the Exec task to run dotnet Microsoft.XmlSerializer.Generator options, and this dotnet command uses a non-preview version of .NET Runtime, e.g. 9.0.10, which then cannot load your application assembly and its references.
If you set DOTNET_ROLL_FORWARD_TO_PRERELEASE=1 in the environment, then it runs the tool in .NET 10.0.0-rc.2 instead, and generates the XML serializers OK.
I think, when .NET Runtime 10.0.0 is released and you install that, Microsoft.XmlSerializer.Generator will start working without such tricks.
https://github.com/dotnet/runtime/issues/69435#issuecomment-1397717917 suggested changing Microsoft.XmlSerializer.Generator to define custom MSBuild tasks instead of using DotNetCliToolReference and launching a dotnet child process. I think that would fix this problem for good.
I can confirm setting DOTNET_ROLL_FORWARD_TO_PRERELEASE=1 fixes the immediate issue, serializers are now generated properly.
Thanks a lot!
We are having the same issue and also asked about it here: https://github.com/dotnet/core/discussions/10122#discussioncomment-14740271
Another workaround might be to move the XML-serializable types to a separate project that targets net8.0 or net9.0, then reference that in the applications that target net10.0.