开发者

Why does ASP.NET web site reference assembly from GAC, when adding local reference?

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.

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.


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.

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.

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

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.


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.refresh file in your /bin folder, where xxxx is the offending dll you are referencing.

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.


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.

The project-assigned value of CopyLocal is determined in the following order:

  1. If the reference is another project, called a project-to-project reference, then the value is true.
  2. If the assembly is found in the global assembly cache, the value is false.
  3. As a special case, the value for the mscorlib.dll reference is false.
  4. If the assembly is found in the Framework SDK folder, then the value is false. Otherwise, the value is true.

Hope this helps

s


For information on how to set the Copy Local Property of a Reference for a web project (not a web application), see:

http://msdn.microsoft.com/en-us/library/t1zz5y8c(v=VS.100).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜