Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transitive references in .Net Core 1.1

While developing a sample web app in .NET Core 1.1 and Visual Studio 2017 RC, I realized the following:

enter image description here

As you can see:

  • ClassLibrary3 has a reference to ClassLibrary2,
  • and ClassLibrary2 has a reference to ClassLibrary1

I wrote a simple method in class Class3 of ClassLibrary3 project, and the Intellisense allowed me to use Class1 just writing the name of the class, I mean, without doing an explicit reference to ClassLibrary1 project.

Am I missing some point here? I don't want somebody simply comes and overlooks ClassLibrary2.

Thanks.

like image 797
Franco Avatar asked Feb 24 '17 00:02

Franco


People also ask

What is transitive reference?

When ReSharper speaks of a transitive reference, it means a reference to an assembly that is made implicitly by referencing another, intermediary assembly. For example, say I have a project with an assembly "Core. dll". I create a project called "Features. dll" that references Core.

What is a transitive dependency package?

Transitive dependencyA dependency that your package indirectly uses because one of its dependencies requires it. If your package depends on A, which in turn depends on B which depends on C, then A is an immediate dependency and B and C are transitive ones.


2 Answers

Transitive project-to-project references are a new feature of Visual Studio 2017 and Microsoft.NET.Sdk. This is intentional behavior.

See https://github.com/dotnet/sdk/issues/200.

like image 77
natemcmaster Avatar answered Oct 21 '22 22:10

natemcmaster


If you're interested in disabling the transitive reference behavior, I finally found a way.

If you want Project A to reference B and B to reference C, but don't want A to reference C, you can add 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

like image 40
levihassel Avatar answered Oct 21 '22 20:10

levihassel