visual-studio Visual Studio 如何使用多项目解决方案确定要复制到输出目录的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/353617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:59:51  来源:igfitidea点击:

How does visual studio determine what to copy to the output directory with multi-project solutions?

visual-studiomsbuildassemblies

提问by Josh Kodroff

Let's say we have a solution with the following structure:

假设我们有一个具有以下结构的解决方案:

  • Project.DAL - Data access layer, depends on a lower-level library, e.g. Oracle.DataAccess w/copy local = true
  • Project.BLL - Business logic layer, references Project.DAL as project
  • Project.UI - UI layer, compiles to executable, references Project.BLL, default project
  • Project.DAL - 数据访问层,依赖于较低级别的库,例如 Oracle.DataAccess w/copy local = true
  • Project.BLL - 业务逻辑层,引用 Project.DAL 作为项目
  • Project.UI - UI 层,编译为可执行文件,引用 Project.BLL,默认项目

When Project.UI is compiled, VS is smart enough to copy Project.DAL.dll to the output directory, but it's not smart enough to figure out that I wanted Oracle.DataAccess to be copied to the output directory as well for distribution to clients.

编译 Project.UI 时,VS 足够聪明,可以将 Project.DAL.dll 复制到输出目录,但它不够聪明,无法弄清楚我希望将 Oracle.DataAccess 复制到输出目录以及分发给客户端.

Can anyone explain why this is so? Is it because it sees Oracle.DataAccess in the GAC and assumes that clients will have it in the GAC as well?

谁能解释为什么会这样?是因为它在 GAC 中看到 Oracle.DataAccess 并假设客户也会在 GAC 中拥有它吗?

It's not that big of a deal, but it's kinda annoying that every time I add a new assembly reference, I have to remember to set it to copy local and add an item to copy it in my build script as well.

这没什么大不了的,但是每次我添加一个新的程序集引用时,我都必须记住将它设置为复制本地并添加一个项目以将其复制到我的构建脚本中,这有点烦人。

采纳答案by Magnus Akselvoll

Yes, Visual Studio will copy a DLL to the output path in any of the two conditions below:

是的,在以下两种情况中的任何一种情况下,Visual Studio 都会将 DLL 复制到输出路径:

  1. The DLL is referenced explicitly with CopyLocal = true
  2. The DLL is referenced without CopyLocal or implicitly through some other referenced DLL and is not in the GAC
  1. DLL 使用 CopyLocal = true 显式引用
  2. DLL 是在没有 CopyLocal 的情况下引用的,或者是通过其他一些引用的 DLL 隐式引用的,并且不在 GAC 中

The reason why it will not copy-local when the file is in GAC, is that when resolving assembly names the GAC has highest priority, i.e. even if you have a (different) local copy, the version from the GAC will be used.

当文件在 GAC 中时它不会复制本地的原因是当解析程序集名称时 GAC 具有最高优先级,即即使您有(不同的)本地副本,也将使用来自 GAC 的版本。

I suggest you set up a library directory where you put all external assemblies that are referenced. Then you set up an automatic MSBuild script on a computer (or VM) that does not have the Oracle-file gac'ed (nor Visual Studio installed for that sake). That way, the file will be copied to the build, and you will have more control over what is done than when using VS.

我建议您设置一个库目录,在其中放置所有引用的外部程序集。然后,您在未安装 Oracle 文件(也未为此安装 Visual Studio)的计算机(或 VM)上设置自动 MSBuild 脚本。这样,文件将被复制到构建中,并且与使用 VS 时相比,您可以更好地控制所做的工作。

回答by Jonathan

One more thing.

还有一件事。

When you don't use the referenced DLL in your codeat all, it will ignore the CopyLocal and won't copyit to your output directory.

当您在代码根本不使用引用的 DLL时,它将忽略 CopyLocal 并且不会将复制到您的输出目录。

回答by Shaun

I had a strange situation where even though the assembly was a project reference and was referenced with "Copy Local" showing up as "True" in the reference properties window, the DLL was not being copied to the output directory. I had an earlier version of the DLL in the GAC but I didn't see why this should prevent the DLL being copied.

我遇到了一个奇怪的情况,即使程序集是一个项目引用并且被引用为“Copy Local”,在引用属性窗口中显示为“True”,但 DLL 没有被复制到输出目录。我在 GAC 中有一个早期版本的 DLL,但我不明白为什么这会阻止复制 DLL。

I found that by unloading the project and manually editing the project reference XML as follows:

我发现通过卸载项目并手动编辑项目引用XML如下:

<ProjectReference Include="..\SomeProject.csproj">
  <Project>{11111111-1111-1111-1111-111111111111}</Project>
  <Name>Some Project Name</Name>
  <Private>True</Private>
</ProjectReference>

The DLL was copied to the ouput directory as expected. I found that just setting Copy Local to True in the properties window meant the <Private>element was completely missing, but in the case of it being set to false it was present with a value of "False".

DLL 已按预期复制到输出目录。我发现只是在属性窗口中将 Copy Local 设置为 True 意味着该<Private>元素完全丢失,但在将其设置为 false 的情况下,它的值为“False”。

回答by crunchy

@RenniePet Here's a link to a blog which describes the method RenniePet described in a comment above (if you don't want to edit your project file manually as @Shaun suggested):

@RenniePet 这是一个博客链接,该链接描述了上面评论中描述的 RenniePet 方法(如果您不想像@Shaun 建议的那样手动编辑您的项目文件):

http://blogs.msdn.com/b/jjameson/archive/2009/11/18/the-copy-local-bug-in-visual-studio.aspx

http://blogs.msdn.com/b/jjameson/archive/2009/11/18/the-copy-local-bug-in-visual-studio.aspx