5

Inside my solution using ASP.NET Core 2.0, I have three projects: A, B and C. My desired behavior for the project dependencies is for A to depend on B, B to depend on C and C to have no dependencies.

In ASP.NET MVC 5, this was easily achievable by setting A to reference B and B to reference C. However in Core, when a project depends on another project, it also inherits all of that project's dependencies. In this case, that means that A inherits B's dependency on C. This dependency can be seen in Visual Studio's Solution Explorer here: .

I want A to depend on B, but none of B's dependencies, like this: .

Does anyone know how to accomplish my desired behavior, either through Visual Studio or the .csproj file? Thanks!

levihassel
  • 356
  • 4
  • 11
  • Isn't one of the new features of 2.0 that when you build the realease version of a project, only the dependencies that it uses is being refrenced? Although i'm not sure if it fully resolves your problem, but you might look into how that works. – Johan Herstad Sep 03 '17 at 08:09
  • @JohanHerstad Thanks, I've looked into this but don't think it will solve my problem. One of the biggest reasons I want to set my project up this way is so that it will never even suggest **using ProjectC** statements in "Show Potential Fixes". This will make it impossible for any developer working on Project A to reference Project C by accident. – levihassel Sep 04 '17 at 19:22

2 Answers2

8

I figured it out.

In order for Project A to reference B and B to reference C, but A to not reference C, I added PrivateAssets="All" to B's ProjectReference to C, like so:

In B.csproj

<ItemGroup>
  <ProjectReference Include="..\C\C.csproj" PrivateAssets="All" />
</ItemGroup>

This setting makes C's reference private so it only exists within B. Now projects that reference B will no longer also reference C.

Source: https://github.com/dotnet/project-system/issues/2313

levihassel
  • 356
  • 4
  • 11
0

If you want the same behavior like in .NET Framework and ASP.NET MVC 5 projects then you can create this Directory.Build.props file in the root folder.

<Project>
  <PropertyGroup>    
    <DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences>
  </PropertyGroup>
</Project>

If you want more detailed explanation you can read my other answer to similar question.

Mariusz Pawelski
  • 25,983
  • 11
  • 67
  • 80