Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 361
Tony Avatar asked Sep 21 '17 13:09

Tony


1 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>
like image 54
Dag Baardsen Avatar answered Nov 20 '22 00:11

Dag Baardsen