Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing DLL from GAC in Visual Studio

So I've looked around to try to find some posts on this and there are many but none that address my specific question (that I could find).

I am trying to add some DLL's in my project but few of them are coming from :

C:\Windows\Microsoft.NET\Framework\v3.5\XXX.YYY.dll

and what I expecting this should be coming from GAC.

Please suggest me the best practice to reference the Dll's in Visual Studio.

like image 478
Haseeb Akhtar Avatar asked Feb 15 '13 09:02

Haseeb Akhtar


People also ask

Where is GAC DLL?

It is located in %windir%\assembly (for example, C:\WINDOWS\assembly) and it is a shared repository of libraries.

How do I view the contents of a GAC?

Use the global assembly cache tool (gacutil.exe) to view the contents of the global assembly cache (GAC).


1 Answers

That's not the way it works. When you use Project + Add Reference then you always add a reference assembly. This is never an assembly from the GAC. The GAC is a runtime implementation detail, it is only ever used to supply assemblies when your program executes, never when it is built.

It is very important that it works that way, the content of the GAC on your machine will not match the content of the GAC on your user's machine. Lots of DLL Hell countermeasures are in place to ensure the mapping of your reference assembly to the user's GAC content is taken care of with good diagnostics when the user's machine isn't configured correctly to execute your program.

This is also the reason that you cannot directly look at the GAC folders when you navigate to c:\windows\assembly with Explorer. A shell extension handler hides the details to stop you from making a mistake like adding a GAC-ed assembly as a reference assembly. This same extension handler is not installed for the .NET 4 assemblies, you can look at c:\windows\microsoft.net\assembly and see the structure of the GAC. Do not assume that it is now okay to add references from there, reference assemblies are even more important in .NET 4, they are completely different from the runtime assemblies.

So seeing the reference assembly stored in C:\Windows\Microsoft.NET\Framework\v3.5 is completely normal, that's the home directory for .NET 3.5 specific reference assemblies, like System.Core.dll. For .NET 4 projects the reference assemblies are stored in c:\program files\reference assemblies, they should not reference C:\Windows\Microsoft.NET\Framework\v4.0.30319. Check this answer to see what kind of undiagnosable misery can be caused by not using the correct reference assemblies.

like image 118
Hans Passant Avatar answered Oct 11 '22 02:10

Hans Passant