Cannot load a reference assembly for execution

There's a known issue with .NET Framework 4.7.1 in this regard. Supposedly sorted in 4.7.2, but in the meantime what can you do?

The issue is related to the serialization assemblies, which you can optionally set to generate or not as part of your build (rclick project -> Properties -> Build tab -> see 'Generate serialization assemblies' at bottom.)

What worked for me - and I'm standing on the shoulders of others in part here - is to ensure this setting is set to 'Auto'. Do a full 'clean solution', and additionally if paranoid, this PowerShell snippet is handy if run in your solution root folder:

Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

Next, add the following targets to your csproj (just inside the enclosing <Project> tag:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  </Target>
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
  </Target>

If still no luck, explicitly set 'Generate serialization assemblies' to 'On' to force generation, and rebuild & run.


What worked for me was deleting the bin and obj directories under my web app then rebuilding.