Click here to Skip to main content
16,018,525 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
I have been searching for benefit of adding assembly in GAC.
I have created an assembly suppose 'myGac' then sign it and added it to GAC using GacUtil. Here i am using .Net 4.0 so my assembly is now in 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL' folder. Now I wanted myGac to be available at 'add reference' dialog box of visual studio 2010 for that I add myGac location to 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NetFramework\Version}\AssemblyFoldersEx\'. Now i create one console application and add the reference of myGac using 'add reference' dialog box, it works well.
I am confused why i did all this things like (sign an assembly, add it to GAC, modify regedit), even i can add reference from local drive mean a local assembly. Ok msdn and all other tutorial say it will be shareable for many application run in same machine.
1. How it is shareable??
2. If i try to make one setup file of this console application the myGac appears in 'Application Folder' this thing happen with local assembly also. How this myGac is different from local assembly.
Please do not post any link, i already have searched most of them. I m here looking for some brief description or example, if any one have.
Thank U
Posted

1 solution

A rule of thumb would be: you can benefit from putting some assembly to GAC, if you 1) have an assembly which can be referenced by more than one product, 2) multiple products using the same assembly are optional; some may be installed or not.

Also, assemblies installed in GAC are referenced by its strong names. Referencing them in projects using them should never involve using any path names of their executable modules (PE files). This is how GAC assemblies are different from local. The are added by the Gacutil.exe: http://msdn.microsoft.com/en-us/library/aa309379%28v=vs.71%29.aspx[^].

See also:
http://en.wikipedia.org/wiki/Global_Assembly_Cache[^],
http://msdn.microsoft.com/en-us/library/yf1d93sz%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/6axd4fx6%28v=vs.110%29.aspx[^].

You should also consider downside of them. They may contaminate the target system. Imagine you have two applications, "productA" and "productB", both using the assembly "myLibrary". You can install them independently in any order, and the first of product installation should also install the assembly "myLibrary". Now, you uninstall "productA" (not necessarily in the same order as installation). As "productB" is still used, "myLibrary" should remain in GAC. Now, you need to install "productB". You should remove "myLibrary', to prevent GAC contamination with unused assemblies. But how to detect if there are no more products still using "myLibrary"? You have to care about all that in your installations.

I, for example, sometimes use an alternative local approach, using application configuration. I put my products using some assembly "myLibrary" in separate sub-directories, and "myLibrary" in some sub-directory shared with all such products using relative path prescribed in application config files (say, some application "myProduct.exe" will need a configuration file named "myProject.exe.config"). Such configuration file can look like this:
XML
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath=".\mySharedLibraries\myLibrary"/>
        </assemblyBinding>
    </runtime>
</configuration>


—SA
 
Share this answer
 
v2
Comments
BillWoodruff 25-Nov-13 17:05pm    
+5 An excellent overview thanks ! I had assumed that the "strong name" requirement was there to explicitly prevent assembly duplication, and name conflict: is that incorrect ?
Sergey Alexandrovich Kryukov 25-Nov-13 17:26pm    
Thank you, Bill.

Preventing duplication/conflicts? You are right. Well, strong naming is quite a strong way to avoid conflicts. :-)

Strong naming have different purposes, one of them is a kind of world-unique identification of an assembly. Consider you are checking a full public key (not even a public key token). Say, you want to create the same key pair (create from scratch, when the private key is not known) intentionally, to fake some existing assembly. Not that the probability of success is strictly zero, but doing such thing would be computationally infeasible, in the cryptographic sense of this explained here (please see the second paragraph): http://en.wikipedia.org/wiki/Public-key_cryptography.

And using strong naming is a must for GAC, but GAC is some secondary purpose, more related to packaging, referencing on an assembly inside a local computer and installation. It is pretty obvious if the uniqueness of the assembly is cryptographically strong, "strong enough" to guarantee world uniqueness with reasonable dependability, it apparently should be "cryptographically strong enough" to provide unique identification of an assembly on the same computer. :-)

So, I always recommend using strong naming even if none of the product components are put in the GAC (sorry, forget to mention about it in my answer above.) We have a saying "Nothing costs as little and valued as much as politeness", so the same thing could be said about strong naming, which can be added in a few seconds (but, if you want to strong name application assembly, you will be forced to strong-name all the referenced assemblies, which is a certain limitation, because some not-so-wise 3rd-party software vendors provide neither source code nor strong name).

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900