Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 4.6.1/.NET Standard 2.0 build with SGen fails

See original GitHub issue

The context is a .NET Standard 2.0 targeted library Lib. It is referenced by a .NET Framework 4.6.1 targeted library LibNetFramework. (see GitHub repository:

In LibNetFramework, serialization assemblies generation is forced by the use of the GenerateSerializationAssemblies property set to On. Typically, this would happen when adding a WebReference to the project.

The compilation fails with the message

SGEN : error : An attempt was made to load an assembly with an incorrect format: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\ref\netfx.force.conflicts.dll.

The issue might have been corrected by the use of the patch in, that is put ine the LibNetFrameworkPatched project in the GitHub repo.

But this breaks the build, as the netstandard.dll reference is no longer added to the compilation:

LibNetFrameworkPatched\Class1.cs(9,30,9,34): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

You can find an appveyor build failing on the VS2017, and VS2017 preview:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:45 (9 by maintainers)

github_iconTop GitHub Comments

mouadhtrabelsicommented, Jan 18, 2018

I I got this error only in project having web service refrences @tomek14 to fix it i disabled GenerateSerializationAssemblies on teh csproj i added this line for all the build config <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
joperezrcommented, Feb 5, 2018

I believe we do know now what is happening here. In order for SGen to work correctly on 4.7.1, it will require one of two possible solutions:

  • Add binding redirects to it’s config file. (This is the least invasive to your project, but it does mean modifying things inside VisualStudio installation, which might break things once you upgrade to the next version of .NET, so I would advise option 2)
  • Add a couple of targets to your .csproj file to make sure we don’t include the design time facades when generating the serialization assemblies. In order to go with this option, simply add the following code right before the </Project> tag.
  <Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />

Read more comments on GitHub >

github_iconTop Results From Across the Web

SGEN error on Build of Release version of mixed ASP.Net ...
The error stems from confusion between NETStandard and NuGet libraries when resolving dlls. Put this into your failing project's .csproj ...
Read more >
See More
The context is a .NET Standard 2.0 targeted library Lib. It is referenced by a .NET Framework 4.6.1 targeted library LibNetFramework.
Read more >
Add a reference to assembly netstandard error with .NET ...
A .NET Framework 4.6.1 project which has a reference to a nuget package built with .netstandard2.0 is no more building.
Read more >
Using .NET Standard with Full Framework .NET - Rick Strahl
NET Framework that is .NET Standard 2.0 compliant is .NET 4.6.1. 4.6.1 through 4.7.1 are all partially compliant with the shipped Runtime, but ......
Read more >
Running an ASP.NET Core application targeting . ...
Recently, I've came across an interesting challenge that was to dockerize an ASP.NET Core 2.2 application targeting .NET Framework 4.7.2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found