Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Framework 4.8 build with SGen.exe fails when referencing a .NET Standard 2.0 project #23225

Open
MaaadMax opened this issue Jan 3, 2022 · 3 comments
Assignees
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@MaaadMax
Copy link

MaaadMax commented Jan 3, 2022

Describe the bug

After I have ported some class libraries to .NET Standard 2.0, I get the following error message when building a .NET Framework 4.8 project which references a .NET Standard 2.0 project:
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'.

After some investigation I found out that the build only fails when GenerateSerializationAssemblies is set to On and SGenUseProxyTypes is set to false in the .NET Framework project. When I set SGenUseProxyTypes to true or remove it the build succeeds.

I hope this is the right place for this bug report.

To Reproduce

  • Create a new class library with .NET Framework 4.8 and rename the default class to NetFramework
  • Add the following two lines to the *.csproj file of the .NET 4.8 project (to the first PropertyGroup)
    <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
    <SGenUseProxyTypes>false</SGenUseProxyTypes>
  • Create a new class library with .NET Standard 2.0 and rename the default class to NetStandard
  • In the .NET 4.8 Project add a reference to the .NET Standard 2.0 class library
  • Create a public property in the .NET 4.8 class library with the type NetStandard
    public NetStandard.NetStandard NetStandard { get; set; }
  • Build the solution in visual studio -> build fails

Attached you'll find a small repro with both projects.

Exceptions

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'

Further technical details

  • dotnet --info:

.NET SDK (reflecting any global.json):
Version: 6.0.101
Commit: ef49f62

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.101\

Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Jan 3, 2022
@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Jan 3, 2022

The error happens regardless of whether the project targeting .NET 4.8 is a legacy project or a .NET SDK project.

MSBuild runs C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\sgen.exe with a /reference option that includes C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Facades\netstandard.dll, but sgen.exe then runs csc.exe with a series of /R options that does not include /R:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Facades\netstandard.dll". If I run csc.exe directly, with the same command line plus that option, then it succeeds.

I think this would need a fix in sgen.exe, but is that even open source?

@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Jan 3, 2022

#10255 (comment) suggests using Microsoft.XmlSerializer.Generator instead of sgen.exe. If I remove the GenerateSerializationAssemblies MSBuild property, and add both a PackageReference and a DotNetCliToolReference as instructed in https://docs.microsoft.com/dotnet/core/additional-tools/xml-serializer-generator, then the build succeeds and the generated serializer does read the Class1.NetStandard property.

It's a bit surprising though; I thought DotNetCliToolReference was being deprecated (#3115) and replaced with tool manifests. dotnet/docs#26253 has been filed.

@MaaadMax
Copy link
Author

#10255 (comment) suggests using Microsoft.XmlSerializer.Generator instead of sgen.exe. If I remove the GenerateSerializationAssemblies MSBuild property, and add both a PackageReference and a DotNetCliToolReference as instructed in https://docs.microsoft.com/dotnet/core/additional-tools/xml-serializer-generator, then the build succeeds and the generated serializer does read the Class1.NetStandard property.

It's a bit surprising though; I thought DotNetCliToolReference was being deprecated (#3115) and replaced with tool manifests. dotnet/docs#26253 has been filed.

With the XML serializer generator it actually works.
It isn't the best solution because I have to update 20+ projects but if SGEN.exe won't be ported to .NET Core, it may be the only solution.

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

3 participants