2

I have some issues with project referencing netstandard projects from net462 projects.

When I run tests in MyTests, it's complaining about missing dlls, those are the dlls from MyLogging NuGet references (SeriLog). I don't want MyTests to make a NuGet reference to MyLogging as that defeats the purpose of it, neither do I want to include the tests in MyLogging. Is there a way make a project reference automatically include all sub references? I'd rather not include a refence to MyLogging dependencies in MyTests, as I'm not interested in testing the how, just that it correctly logs to whatever it should based on configuration. Including MyLogging dependencies in the test project is leaking the implementation, and I will have to maintain references if I decide to log in a difference way.

MyLogging

  • target netstandard1.3
  • published as NuGet to be consumed by other projects
  • Dependencies (here SeriLog) are referenced as a NuGet package

csproj file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.3</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
    <PackageReference Include="Serilog" Version="2.4.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
    <PackageReference Include="Serilog.Sinks.ColoredConsole" Version="2.0.0" />
  </ItemGroup>
</Project>

MyTests

  • target net462
  • project reference to MyLogging.

csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="4.19.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
    <PackageReference Include="NSubstitute" Version="2.0.3" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.analyzers" Version="0.1.0" />
    <PackageReference Include="xunit.categories" Version="1.0.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="MyLogging\MyLogging.csproj" />
  </ItemGroup>
</Project>

If I use the dotnet cli tooling, I can run dotnet test and it runs without issue, so I assume that it's VS2017 that gets confused with the new project format.

hvidgaard
  • 495
  • 2
  • 13
  • Have you looked at the generated nuspec for MyLogging to see if it includes the dependencies? There have been various bugs in that step which can make them wrong or missing (through no fault of yours). – Marc Gravell Jun 01 '17 at 13:24
  • Dependencies should already flow transitively for project references. Please check if the serilog binaries are present in your test project's bin directory. But not that .net framework test projects do not generate binding redirects by default so you might hit an assembly binding error. See related https://stackoverflow.com/questions/43995432/could-not-load-file-or-assembly-microsoft-extensions-dependencyinjection-abstrac/43996389#43996389 – Martin Ullrich Jun 01 '17 at 15:35
  • @MarcGravell It is listed in the nuspec, but I'm not using the NuGet package, but rather a project reference. – hvidgaard Jun 02 '17 at 05:46
  • @MartinUllrich The binaries are not in the logging project. Since it's a .netstandard project, all dependencies are listed in the .deps.json file. That might be why the tooling is not picking them up. Any suggestions on how to remedy this? – hvidgaard Jun 02 '17 at 05:48

1 Answers1

3

For old style projects (old .csproj format) you have to add the following to the .csproj file:

<PropertyGroup>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>

This doesn't answer the original question, but since this was the issue I faced I'm leaving the answer for the benefit of others.

See: Referencing .NET Standard Assemblies from both .NET Core and .NET Framework

TommyN
  • 2,252
  • 22
  • 19