复制本地如何工作?log4net.dll 未复制到 MyProject 输出目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/320295/
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
How does Copy-local work? log4net.dll is not being copied to MyProject output directory
提问by Fadeproof
I am wondering what copy-local=true for references exactly does. Does it copy the referenced assembly along with all of its dependencies to the output directory?
我想知道引用的 copy-local=true 到底是做什么的。它是否将引用的程序集及其所有依赖项复制到输出目录?
My scenario is the following: I have a custom log wrapper that utilizes log4net. I build a release assembly of MyLogWrapper.dll with log4net.dll reference set to copy-local true. Referencing MyLogWrapper.dll from MyProject with copy local set to true should result in log4net.dll being copied as well right? I am only referencing MyLogWrapper.dll and none of its dependencies in MyProject. log4net.dll is not being copied to MyProject output directory but all other dependencies of MyLogWrapper are. What could be the problem?
我的场景如下:我有一个使用 log4net 的自定义日志包装器。我构建了一个 MyLogWrapper.dll 的发布程序集,其中 log4net.dll 引用设置为 copy-local true。从 MyProject 引用 MyLogWrapper.dll 并将复制本地设置为 true 应该会导致 log4net.dll 也被复制,对吗?我只引用了 MyLogWrapper.dll 而在 MyProject 中没有引用它的任何依赖项。log4net.dll 不会被复制到 MyProject 输出目录,但 MyLogWrapper 的所有其他依赖项都是。可能是什么问题呢?
I have made some more experiments and it seems that if I remove the assembly (log4net.dll) from GAC it starts to get copied locally. Can anyone confirm that this is the problem?
我做了一些更多的实验,似乎如果我从 GAC 中删除程序集(log4net.dll),它就会开始在本地复制。谁能确认这是问题所在?
回答by jpierson
Unfortunately it appears that according to the following statement taken from the MSDN documentationthe CopyLocal functionality does not work as expected for assemblies already in the GAC.
不幸的是,根据MSDN 文档中的以下声明,对于 GAC 中已有的程序集,CopyLocal 功能似乎无法正常工作。
If you deploy an application that contains a reference to a custom component that is registered in the GAC, the component will not be deployed with the application, regardless of the CopyLocal setting. In previous versions of Visual Studio, you could set the CopyLocal property on a reference to ensure that the assembly was deployed. Now, you must manually add the assembly to the \Bin folder. This puts all custom code under scrutiny, reducing the risk of publishing custom code with which you are not familiar.
如果您部署的应用程序包含对在 GAC 中注册的自定义组件的引用,则无论 CopyLocal 设置如何,该组件都不会随应用程序一起部署。在早期版本的 Visual Studio 中,您可以在引用上设置 CopyLocal 属性以确保程序集已部署。现在,您必须手动将程序集添加到 \Bin 文件夹中。这会将所有自定义代码置于之下,从而降低发布您不熟悉的自定义代码的风险。
More information can be found at the following page which explains details about how project references work.
可以在以下页面找到更多信息,该页面解释了有关项目引用如何工作的详细信息。
回答by Fadeproof
After asking this question on MSDN here- it seems that this behaviour is by design. "If you deploy/copy an application that contains a reference to a custom component that is registered in the GAC, the component will not be deployed/copied with the application, regardless of the Copy Local setting."
在此处的MSDN 上询问此问题后- 似乎这种行为是设计使然。“如果您部署/复制包含对在 GAC 中注册的自定义组件的引用的应用程序,则无论复制本地设置如何,该组件都不会与应用程序一起部署/复制。”
回答by JustAMartin
There is a trick: set the reference Copy Local to false and then again to true, and Visual Studio adds the Private metadata automatically for that reference. At least VS 2010 does. I recently did this to solve an issue with our TFS Build server which for some weird reason had many Enterprise Library components installed in the GAC, so we had major issues when deploying our project from TFS Drop folder. That false/true trick saved us.
有一个技巧:将引用 Copy Local 设置为 false,然后再次设置为 true,Visual Studio 会自动为该引用添加 Private 元数据。至少 VS 2010 是这样。我最近这样做是为了解决我们的 TFS 构建服务器的一个问题,该服务器出于某种奇怪的原因在 GAC 中安装了许多企业库组件,因此在从 TFS Drop 文件夹部署我们的项目时遇到了重大问题。那个假/真把戏救了我们。
回答by xan
You need to be a bit wary of copy local as it has caught me out in the past!
您需要对本地复制保持警惕,因为它过去曾让我感到困惑!
Just occasionally, for a particular .dll it will silently fail to copy it to the build folder. Usually this doesn't show up on a dev machine as the dll's are often in the GAC as well (if you have installed a dev tool / library you are using for development) and so you don't notice until it gets distrobuted / bundled into an installer and the required files are missing on a client machine.
只是偶尔,对于特定的 .dll,它会默默地无法将其复制到构建文件夹。通常这不会出现在开发机器上,因为 dll 也经常在 GAC 中(如果您已经安装了用于开发的开发工具/库),所以在它被分发/捆绑之前您不会注意到进入安装程序,并且客户端计算机上缺少所需的文件。
There is not much info on this bug, but this thread demonstrates it for a particular library: here.
关于此错误的信息不多,但此线程为特定库演示了它:here。
Having been caught by this, I think it's a good idea (generally in any case) to know exactly which assemblies are required by your project and to have a script or similar automated action which ensures all required componants are present, either when you build, or more likely when you make an installer, or collect files for distribution,
被这个发现后,我认为这是一个好主意(通常在任何情况下)确切地知道您的项目需要哪些程序集并拥有一个脚本或类似的自动化操作来确保所有必需的组件都存在,无论是在构建时,或者更有可能在您制作安装程序或收集文件以进行分发时,
回答by Raj Kumar
When local copy is set to true it will copy all the assemmbly whose attribute local copy = tue to the bin direcory of the application.
当 local copy 设置为 true 时,它会将属性 local copy = tue 的所有程序集复制到应用程序的 bin 目录。
In your case the dll might be using the other dll so it require that also.
在您的情况下,dll 可能正在使用其他 dll,因此它也需要这样做。
回答by vezenkov
I've found that this is no more respected in Visual Studio 2015 with project references if the referenced project has a dependency to a GAC assembly. The GAC assembly is always copied to the root project output and copy local = false being respected only regarding the output of the project holding the reference to the GAC dll.
我发现如果引用的项目依赖于 GAC 程序集,则在 Visual Studio 2015 中不再尊重项目引用。GAC 程序集始终复制到根项目输出中,并且复制 local = false 仅在涉及对 GAC dll 的引用的项目输出方面受到尊重。

