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
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>
Transitive project references are new feature of SDK-style csproj (1,2) format used in .NET Core/.NET >= 5. You can also use this csproj for old .NET Framework projects (1,2,3) but with some exceptions.
In this SDK-style format project references (represented by <ProjectReference>
entry in .csproj file) are transitive. This is different to old non-SDK .csproj used previously.
But you have two options to go back to old non-transitive behavior.
Use <DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences>
property in .csproj that references projects for which you don't want their transitive dependencies to be visible by compiler.
In your case you can add it to Web project. (first project that reference other projects, Web -> Service -> Business)
You can also set this behavior globally for all .csprojs by doing it in Directory.Build.props file that you put in the root folder that contains your source.
<Project>
<PropertyGroup>
<DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences>
</PropertyGroup>
</Project>
With this file you basically have old project reference behaviour. Useful when you do migration of old .NET Framework solution that uses old csproj format to new SDK-style .csprojs.
On the project that you reference you can set which dependencies shouldn't flow further when the project is referenced. You use PrivateAssets="All"
attribute on <ProjectReference>
for this. So for example you can edit Service.csproj like this:
<ItemGroup>
<ProjectReference Include="..\Business.csproj" PrivateAssets="All" />
</ItemGroup>
This is more flexible and fine-grained approach. You can control with particular transitive project references should be visible when the project is referenced.
It depends what you prefer. If you are used to old csproj behavior or want to migrate old solution to .NET Core then just go with DisableTransitiveProjectReferences
. It's the easiest solution.