Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 889
Konstantin Avatar asked Jan 07 '10 16:01

Konstantin


3 Answers

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.

like image 169
Don Avatar answered Nov 08 '22 04:11

Don


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.

like image 21
LordHits Avatar answered Nov 08 '22 02:11

LordHits


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

like image 41
BALKANGraph Avatar answered Nov 08 '22 04:11

BALKANGraph