.net 什么时候应该将 copy-local 设置为 true,什么时候不应该?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602765/
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-09-03 12:14:55  来源:igfitidea点击:

When should copy-local be set to true and when should it not?

.netmsbuildreferencecopy-local

提问by Fadeproof

I am wondering if there are any heuristics for when to set copy-local=truefor references?

我想知道何时设置copy-local=true参考是否有任何启发式方法?

If referenced types are only used internally can I set copy-localto truebut if referenced types are exposed as parameters or return values I set copy-localto falseand indicate that a specific version of the dependency should be referenced when my library should be used?

如果引用类型仅在内部使用,我是否可以设置copy-local为,true但如果引用类型作为参数或返回值公开,我设置copy-localfalse并指示在使用我的库时应引用依赖项的特定版本?

Can anyone clarify this for me?

任何人都可以为我澄清这一点吗?

回答by JaredPar

Copy local is important for deployment scenarios and tools. As a general rule you should use CopyLocal=True if the reference is not contained within the GAC.

复制本地对于部署场景和工具很重要。作为一般规则,如果引用不包含在 GAC 中,您应该使用 CopyLocal=True。

Copy Local essentially means I must manually deploy this DLL in order for my application to work. When it's false it essentially means "I depend on another component which must be installed separately or chained, the DLL will just be there already".

Copy Local 本质上意味着我必须手动部署这个 DLL 才能让我的应用程序正常工作。当它为 false 时,它​​本质上意味着“我依赖另一个必须单独安装或链接的组件,DLL 将已经存在”。

回答by Sayed Ibrahim Hashimi

Copy local was implemented really to support local debugging. When you perpare your application for package and deployment you should build your projects to the same output folder and make sure you have all the references you need there.

真正实现了本地复制以支持本地调试。当你准备你的应用程序进行打包和部署时,你应该将你的项目构建到同一个输出文件夹,并确保那里有你需要的所有引用。

CopyLocal is especially a pain when building large source trees. There was a related question about how to disable CopyLocal here on SO you can see it at How do I override CopyLocal (Private) setting for references in .NET from MSBUILD. As well as Best practices for large solutions in Visual Studio (2008).

CopyLocal 在构建大型源代码树时尤其痛苦。有一个关于如何在此处禁用 CopyLocal 的相关问题,因此您可以在How do I override CopyLocal (Private) setting for references in .NET from MSBUILD 中看到它。以及Visual Studio (2008) 中大型解决方案的最佳实践。

I have written about how to deal with building large source trees in the article MSBuild: Best Practices For Creating Reliable Builds, Part 2.

我在文章MSBuild:创建可靠构建的最佳实践,第 2 部分 中撰写了有关如何处理构建大型源代码树的文章。

So in short I would say disable CopyLocal when the file copying is causing your builds to take more time then you are willing to spend for every build.

所以简而言之,当文件复制导致您的构建花费更多时间时,我会说禁用 CopyLocal,而您愿意为每个构建花费更多时间。

回答by Kent Boogaart

It's really about the target environment. If copy local is false, you're saying that the assembly will already exist in the target environment (normally in the GAC). Setting it to true ensures it will appear in the output of your build, so makes it easier to deploy to the target environment.

这实际上与目标环境有关。如果 copy local 为 false,则表示该程序集已存在于目标环境中(通常在 GAC 中)。将其设置为 true 可确保它出现在您的构建输出中,因此可以更轻松地部署到目标环境。

回答by jpierson

Check out the following MSDN reference which explains CopyLocal behavior in detail.

查看以下详细解释 CopyLocal 行为的 MSDN 参考。

Project References

项目参考

Unfortunately there are some quirks and CopyLocal won't necessary work as expected for assembly references in secondary assemblies structured as shown below.

不幸的是,有一些怪癖,对于如下所示结构的辅助程序集中的程序集引用,CopyLocal 不需要按预期工作。

  • MainApp.exe
    • MyLibrary.dll
      • ThirdPartyLibrary.dll (if in the GAC CopyLocal won't copy to MainApp bin folder)
  • 主应用程序
    • 我的图书馆.dll
      • ThirdPartyLibrary.dll(如果在 GAC CopyLocal 不会复制到 MainApp bin 文件夹)

This makes xcopy deployments difficult if you don't plan on installing the third party assembly into the GAC on the target machine.

如果您不打算将第三方程序集安装到目标机器上的 GAC 中,这会使 xcopy 部署变得困难。

回答by Mehrdad Afshari

This option only affects build phase. It just copies the reference to local directory of the built assembly.

此选项仅影响构建阶段。它只是将引用复制到构建的程序集的本地目录。

If another assembly (T) wants to use a method from the assembly you are building (A) which has return type or parameters from another referenced assembly (R), it (T) should be able to access that assembly (R). It might be able to do so without doing anything special if the referenced assembly (R) is installed in GAC. Otherwise, it needs a local copy of that.

如果另一个程序集 (T) 想要使用您正在构建的程序集 (A) 中的方法,该方法具有来自另一个引用程序集 (R) 的返回类型或参数,则它 (T) 应该能够访问该程序集 (R)。如果引用的程序集 (R) 安装在 GAC 中,它可能无需执行任何特殊操作即可执行此操作。否则,它需要一个本地副本。

回答by ollipekka

Conservative way to set CopyLocal false is to check that the reference is found in the output path of the project. This should allow you to dodge some nasty runtime issues, while still reducing the amount of IO.

设置 CopyLocal false 的保守方法是检查是否在项目的输出路径中找到了引用。这应该允许您避免一些讨厌的运行时问题,同时仍然减少 IO 数量。

In the process I created CopyLocalFixer, which you can run for a folder. I tried this with one large build, but the results weren't that impressive to be honest. I guess it comes down to the folder structure of the project.

在这个过程中,我创建了CopyLocalFixer,你可以为一个文件夹运行它。我用一个大型构建尝试了这个,但说实话,结果并没有那么令人印象深刻。我想这归结为项目的文件夹结构。

回答by Michael Freidgeim

Set CopyLocal=false will improve build time, but can cause different issues during deployment time.

设置 CopyLocal=false 将缩短构建时间,但在部署期间可能会导致不同的问题。

My experience with setting CopyLocal=false wasn't successfull. See summary of pro and cons in my blog post "Do NOT Change "Copy Local” project references to false, unless understand subsequences."

我设置 CopyLocal=false 的经验并不成功。请参阅我的博客文章“不要将“复制本地”项目引用更改为 false,除非理解子序列中的利弊摘要。