.NET Core 中的项目引用与以前的 .NET 版本不同吗? [英] Project references in .NET Core different from previous .NET versions?

查看:26
本文介绍了.NET Core 中的项目引用与以前的 .NET 版本不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用 C# 和 dotnet Core 设置新的 Web Api.在引用其他项目时,我注意到一些奇怪的行为.

Currently setting up a new Web Api using C# and dotnet Core. And i notice some strange behaviour when referencing other projects.

我的解决方案非常简单:DataAccess、BusinessLogic、WebApi

My solutions is pretty straight forward: DataAccess, BusinessLogic, WebApi

WebApi 项目引用了 BusinessLogic 项目,BusinessLogic 项目引用了 DataAccess 项目.

The WebApi project has a reference to the BusinessLogic project and the BusinessLogic project has a reference to the DataAccess project.

现在使用以前版本的 .NET,我的 WebApi 项目将无法访问 DataAccess 项目中的任何内容,但在我当前的设置中,我可以访问我的 WebApi 项目中 DataAccess 项目中的任何内容,而无需对其进行引用.

Now with previous versions of .NET my WebApi project would not have access to anything from the DataAccess project but in my current setup i can access anything form the DataAccess project in my WebApi project without having a reference to it.

谁能解释一下这种行为以及如何防止它?

Could anyone please explain this behaviour and possibly how to prevent it?

推荐答案

在此线程中找到了防止这种行为的方法:在 .NET Standard 2 中禁用可传递的项目引用

Found a way to prevent this behaviour in this thread: Disable transitive project reference in .NET Standard 2

编辑项目文件时,PrivateAssets 属性可以设置为 All 以防止这种行为.

When editing the project file the PrivateAssets attribute can be set to All to prevent this behaviour.

  <ItemGroup>
    <ProjectReference Include="..DataAccess1-DataAccess.csproj" PrivateAssets="All" />
  </ItemGroup>

这篇关于.NET Core 中的项目引用与以前的 .NET 版本不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆