visual-studio 添加本地引用时,为什么 ASP.NET 网站从 GAC 引用程序集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2022019/
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
Why does ASP.NET web site reference assembly from GAC, when adding local reference?
提问by Konstantin
When you use ASP.NET web site (instead of web application model) and add reference to an assembly from local folder, Visual Studio, it seems, understands that this local assembly is also in GAC and so does NOT copy this assembly to bin folder (as it does with non-GAC assemblies), but simply adds new record in web.config file.
当您使用 ASP.NET 网站(而不是 Web 应用程序模型)并从本地文件夹添加对程序集的引用时,Visual Studio 似乎理解此本地程序集也在 GAC 中,因此不会将此程序集复制到 bin 文件夹(与非 GAC 程序集一样),但只需在 web.config 文件中添加新记录。
Why such a behaviour? Is it possible to force copy to bin folder (I need this since .dll is not on target environment)? I can add assembly to bin folder as file and it will work, but in this case bin folder contents will be in source control, which is not good.
为什么会有这样的行为?是否可以强制复制到 bin 文件夹(我需要这个,因为 .dll 不在目标环境中)?我可以将程序集作为文件添加到 bin 文件夹中,它会起作用,但在这种情况下,bin 文件夹的内容将在源代码管理中,这并不好。
采纳答案by Don
You can set the Copy Local property to True on the reference. That should add it to the bin folder - on a Web Application project.
您可以将参考的 Copy Local 属性设置为 True。这应该将它添加到 bin 文件夹 - 在 Web 应用程序项目上。
But for a Web Site project, when you add a reference, all it does is add a line to the web.config that references the assembly. It will look for this file first in the bin folder, and then in the GAC if it is not found.
但是对于网站项目,当您添加引用时,它所做的只是在引用程序集的 web.config 中添加一行。它将首先在 bin 文件夹中查找此文件,如果未找到,则在 GAC 中查找。
You have two options: require the assembly to be installed in the GAC on the target machine (in which case, XCOPY deployment is not possible) or include all required assemblies in the bin folder, either by copying them in or writing a post-build script that does so. You can find the .dll by using the command prompt and going to c:\windows\assembly\GAC, find the assembly you are interested in, cd into that directory and then cd into the directory with the version you are interested in. This will give you the path to use in your post-build script. For example, for the Accessibility assembly in the GAC, you'd end up with this path: c:\Windows\assembly\GAC\Accessibility\1.0.5000.0__b03f5f7f11d50a3a\Accessibility.dll
您有两个选择:要求将程序集安装在目标机器上的 GAC 中(在这种情况下,XCOPY 部署是不可能的)或将所有必需的程序集包含在 bin 文件夹中,通过将它们复制或编写后构建这样做的脚本。您可以使用命令提示符找到 .dll 并转到 c:\windows\assembly\GAC,找到您感兴趣的程序集,cd 进入该目录,然后 cd 进入具有您感兴趣的版本的目录。这将为您提供在构建后脚本中使用的路径。例如,对于 GAC 中的辅助功能程序集,您最终会得到以下路径:c:\Windows\assembly\GAC\Accessibility\1.0.5000.0__b03f5f7f11d50a3a\Accessibility.dll
You say including the bin folder contents in source control is not good. This is generally regarded as true for binaries you build, but in your case, you have binary assets that are not compiled as part of your project. Philosophically, these are equivalent to images: binary assets not compiled as part of your project. I would argue they belong in source control as much as any other binary your project relies on. But it is a personal choice.
你说在源代码管理中包含 bin 文件夹内容不好。对于您构建的二进制文件,这通常被认为是正确的,但在您的情况下,您的二进制资产并未作为项目的一部分进行编译。从哲学上讲,这些相当于图像:未编译为项目一部分的二进制资产。我认为它们与您的项目所依赖的任何其他二进制文件一样属于源代码控制。但这是个人选择。
回答by LordHits
If you are using a website project in Visual Studio and a reference keeps pointing it to the GAC version instead of some other folder (eg lib), you will need to create a xxxx.dll.refreshfile in your /bin folder, where xxxx is the offending dll you are referencing.
如果您在 Visual Studio 中使用网站项目并且引用一直将其指向 GAC 版本而不是某个其他文件夹(例如 lib),则您需要xxxx.dll.refresh在 /bin 文件夹中创建一个文件,其中 xxxx 是您使用的有问题的 dll正在参考。
This will resolve build problems with MSBuild too where the server will expect the dll to be in the GAC. The .refresh file will get the file from the correct relative path to do builds correctly.
这也将解决 MSBuild 的构建问题,其中服务器希望 dll 位于 GAC 中。.refresh 文件将从正确的相对路径获取文件以正确构建。
回答by BALKANGraph
At run time, assemblies must be in one of two locations: the output path of the project or the global assembly cache (see Working with Assemblies and the Global Assembly Cache). If the project contains a reference to an object that is not in one of these locations, then when the project is built, the reference must be copied to the output path of the project. The CopyLocal property indicates whether this copy needs to be made. If the value is true, the reference is copied. If false, the reference is not copied.
在运行时,程序集必须位于以下两个位置之一:项目的输出路径或全局程序集缓存(请参阅使用程序集和全局程序集缓存)。如果项目包含对不在这些位置之一的对象的引用,则在构建项目时,必须将该引用复制到项目的输出路径。CopyLocal 属性指示是否需要制作此副本。如果值为真,则复制引用。如果为 false,则不复制引用。
The project-assigned value of CopyLocal is determined in the following order:
CopyLocal 的项目分配值按以下顺序确定:
- If the reference is another project, called a project-to-project reference, then the value is true.
- If the assembly is found in the global assembly cache, the value is false.
- As a special case, the value for the mscorlib.dll reference is false.
- If the assembly is found in the Framework SDK folder, then the value is false. Otherwise, the value is true.
- 如果引用是另一个项目,称为项目到项目引用,则该值为真。
- 如果在全局程序集缓存中找到该程序集,则该值为 false。
- 作为一种特殊情况,mscorlib.dll 引用的值为 false。
- 如果在 Framework SDK 文件夹中找到该程序集,则该值为 false。否则,该值为真。
Hope this helps
希望这可以帮助
s
秒
回答by Chris Halcrow
For information on how to set the Copy Local Property of a Reference for a web project (not a web application), see:
有关如何为 Web 项目(而非 Web 应用程序)设置引用的复制本地属性的信息,请参阅:
http://msdn.microsoft.com/en-us/library/t1zz5y8c(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/t1zz5y8c(v=VS.100).aspx

