Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the access to projects that are not referenced directly in ASP.NET Core

These are my project layers:

.NET Core 2.0 Web Project: UILayer
.NET Standard Class library : BusinessLayer
.NET Standard Class library : DataAccessLayer

I add the reference of BusinessLayer to UILayer and the reference of DataAccessLayer to BusinessLayer

So I expected the layers just had access to the those dlls, that are referenced. But now I see the UILayer has access to DataAccessLayer! Why? And how could I prevent that? (I mean physically not just logically)

It seems it is new in .NET Core I can't see the same functionality with .NET Framework 4.6.1 class libraries. So another question is that why it is added in ASP.NET Core projects? Is there any advantages? If I want to access the project simply I could add the reference.

like image 374
Saeid Avatar asked Oct 16 '25 11:10

Saeid


1 Answers

You can specify that inside the .csproj of your BusinessLayer project, use the <PrivateAssets> element inside the <ProjectReference> to declare the assets that you do not want to be visible for parent projects. Here you have additional info for this element

<ProjectReference Include="{path-of-your-referenced-DataAccessLayer}">
  <PrivateAssets>All</PrivateAssets>
</ProjectReference>
like image 152
animalito maquina Avatar answered Oct 19 '25 02:10

animalito maquina