SGEN error on Build of Release version of mixed ASP.Net and .Net Standard 2.0 projects Solution

Viewed 706

I am working on a Visual Studio 2017 Solution that contains 3 projects:

Two Class Libraries in .Net Standard 2.0 (Any CPU)
One ASP.Net in .Net Framework 4.6.1 (Any CPU)

If I Build All in Debug (Any CPU), all runs fine.
But if I Build All in Release (Any CPU), then this error shows in the Output Window:

3>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.

How to solve it?

2 Answers

The error stems from confusion between NETStandard and NuGet libraries when resolving dlls. Put this into your failing project's .csproj file (Unload project, Edit .csproj file):

<Target Name="ReplaceNetFxNetStandardRefWithLib" AfterTargets="ImplicitlyExpandNETStandardFacades">
  <ItemGroup>
    <Reference Remove="@(_NETStandardLibraryNETFrameworkReference)" Condition="'%(FileName)' != 'netfx.force.conflicts'" />
    <Reference Remove="@(_NETStandardLibraryNETFrameworkReference)" Condition="'%(FileName)' != 'System.Configuration.ConfigurationManager'" />
    <Reference Include="@(_NETStandardLibraryNETFrameworkLib)">
      <Private>true</Private>
    </Reference>
  </ItemGroup>
</Target>
<Target Name="RemoveNetFxForceConflicts" AfterTargets="ResolveAssemblyReferences">
  <ItemGroup>
    <ReferencePath Remove="@(ReferencePath)" Condition="'%(FileName)' == 'netfx.force.conflicts'" />
    <ReferencePath Remove="@(ReferencePath)" Condition="'%(FileName)' == 'System.Configuration.ConfigurationManager'" />
  </ItemGroup>
</Target>

In my case, this error was reported due to incorrect package installed in the project. Though you uninstall the incorrect package installed it was still reporting error, the solution will be

  1. Ensure to uninstall the package with the option "Force uninstall even if there are dependencies on it"
  2. Even after following step 1 if you still encounter the same sgent error, then expand "References" node in solution explorer, remove the unwanted dlls that might have been installed as part of installing the incorrect package.
Related