1

Before ASP.Net Core:

  • I create a MVC site and two class libraries in one solution. Let's say the project names are MVC, Lib1 and Lib2.
  • I add references as follows: MVC references Lib1, Lib1 references Lib2.

With this reference structure, MVC cannot access classes in the Lib2 project, which is as expected and what I want.

The same project and reference structure in ASP.Net Core: MVC can access classes in Lib2. Looking at the MVC/References node in the Solution Explorer in Visual studio, you can see MVC/References/DNX 4.5.1/Lib1 - so far so good (I did add a reference to Lib1 from MVC) - but then I can expand the Lib1 node, and will see Lib2 under it. End result: MVC can access Lib2 classes via a reference chain.

I presume this behaviour is by design, but then how can I achieve the old behaviour? I do not want developers to be able to access Lib2 classes from MVC.

Andrew
  • 1,139
  • 1
  • 12
  • 27
  • Did you really try to access a class from you Lib2 in Mvc proj ? Did it compile ? – Shyju May 14 '16 at 13:39
  • @Shyju yes it compiles, and also runs successfully on IIS Express. Another interesting thing I noticed: if you go in the menu to Project -> Project Dependencies, you will see that MVC depends on both Lib1 and Lib2. With the same reference structure, the pre-ASP.Net Core solution would show that MVC depends on Lib1 only. – Andrew May 15 '16 at 09:01
  • i have: MVC -> AppServices -> DataAccess this is actually true which is a bummer. at compile time this shouldn't be allowed. i'm not sure what the decision for this is. separation of concerns during code writing may be violated because of leaking references that enables you to directly call data access classes. – aiapatag Mar 30 '17 at 23:29
  • @aiapatag I used Lib1 and Lib2 when asking this question, but the reason I asked it is exactly your scenario - my Lib1 is App Services and my Lib2 is Data Access. Apparently transitive project referencing is a new feature now :( http://stackoverflow.com/questions/42428571/transitive-references-in-net-core-1-1 – Andrew Mar 31 '17 at 02:49

1 Answers1

1

Yeah, this is now a feature in .NET Core. Read more from this SO answer. BUT you can hide Lib2 from your MVC csproj using PrivateAssets attribute on your ProjectReference:

in your Lib1.csproj:

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

This way, when the MVC.csproj is referencing Lib1, it won't be able to see any Lib2 class because you hid it in your Lib1.csproj.

aiapatag
  • 3,355
  • 20
  • 24