error CS0430: The extern alias 'snh' was not specified in a /reference option [Up.UnitTests\obj\x64\Debug\Fakes\wsc\f.csproj]

Viewed 1846

Unit test project Up.UnitTests fails during build, with this build errors

error CS0430: The extern alias 'snh' was not specified in a /reference option error CS0234: The type or namespace name 'System' does not exist in the namespace 'snh' (are you missing an assembly reference?)

The type or namespace name 'System' does not exist in the namespace 'snh'

I am guessing the above errors are due to System.Net.Http. Is it anything to do with different versions? How to fix this issue. Any help would be appreciated.

1 Answers

I, also, ran into this CS0430 error. In my scenario, the issue came due to a dependency caused by updating NSubstitute. I updated my *.fakes file like below to add diagnostics and see that the issue was System.Net.Http like you observed.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
  <Assembly Name="ClassLibrary1"/>
</Fakes>

After this, I tried to find the root cause of the issue by slowly removing the new dependencies and found that when System.Threading.Tasks.Extensions Nuget reference was there I would get the build error. During the upgrade NSubstitute added the System.Threading.Tasks.Extensions. In your scenario, it may likely be a similar dependency issue that it is not directly related to System.Net.Http.

Update: I found this issue on the dotnet/sdk repo that fixes the issue. To your test project, add the following Directory.Build.targets.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="PrivateFakesAfterFacadesSwitch"
          Condition="'$(ImplicitlyExpandNETStandardFacades)' == 'true' and $(AssemblyName.EndsWith('.Fakes'))"
          AfterTargets="ImplicitlyExpandNETStandardFacades" DependsOnTargets="ImplicitlyExpandNETStandardFacades">

    <ItemGroup Condition="'@(_NETStandardLibraryNETFrameworkLib)' != ''">
      <SnhReference Include = "@(_NETStandardLibraryNETFrameworkLib)" Condition="'%(_NETStandardLibraryNETFrameworkLib.FileName)' == 'System.Net.Http'" />

      <Reference Remove="%(SnhReference.FileName)" Condition="'@(SnhReference)' != ''"/>
      <Reference Include="%(SnhReference.FileName)" Condition="'@(SnhReference)' != ''">
        <HintPath>%(SnhReference.Identity)</HintPath>
        <Private>false</Private>
        <Aliases>snh</Aliases>
      </Reference>
    </ItemGroup>

  </Target>

</Project>
Related