levihassel
levihassel

Reputation: 356

How do I exclude project dependencies in ASP.NET Core?

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!

Upvotes: 5

Views: 5563

Answers (2)

Mariusz Pawelski
Mariusz Pawelski

Reputation: 28972

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.

Upvotes: 0

levihassel
levihassel

Reputation: 356

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

Upvotes: 8

Related Questions