Search code examples
c#asp.net-core.net-standard.net-standard-2.0

Disable transitive project reference in .NET Standard 2


I'm writing an MVC website using ASP.NET Core 2.0.

In the ASP.NET Core project (let's call it Web), I reference a .NET Standard 2 project in the same solution (let's call it Service). The Service project also references a third .NET Standard 2 library in the solution (let's call this one Business). The Business project declares a type called Model.

The problem is that I can use Model in the Web project (i.e. the compiler sees the type Model and I can do var a = new Model();) as if the Web project has referenced Business, but it actually only has a reference to Service.

How can I hide Model from Web? Is this a new feature in ASP.NET Core 2 or all .NET Standard projects are like this?

Edit

As specified here, this is due to transitive project references which is a new "feature" in .NET Standard, but how do I fix it?


Solution

  • Well my question was close to one marked as duplicate here but to solve it requires different tactic.

    Thanks to comment from "Federico Dipuma" and the answer given here, I was able to solve this problem.

    You should edit the Service.csproj file and add PrivateAssets="All" to ProjectReference keys you don't want to flow to top.

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