Search code examples
c#asp.net-coredependenciesprojects-and-solutionsasp.net-core-2.0

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!


Solution

  • 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