This article transferred from: http://www.cnblogs.com/jhxk/articles/2564295.html
1. What is the GAC? What issues does the GAC solve?
The GAC is all called: Global Assembly Cache (Universal assembly caching), you can see the GAC installed on your computer's system disk such as: C:\Windows\assembly\.
The GAC's role is to store some common assembly (assemblies) that are used by multiple applications, such as System.Data, System.Windows.Forms, and so on. This allows multiple applications to get Assembly from the GAC without having to copy the Assembly used in all applications to the relevant directory of the application (for example, the debug directory for Windows applications, ASP. NET's Bin directory).
To cite one of the simplest examples, many beginners. NET developers for data access are starting from SqlHelper.cs, SqlHelper is a data access object, providing a variety of methods for database operations, the name is probably learned from PetShop bar, look like this, or this appearance, and no matter what kind, If more than one application deployed on the server uses this object, there are basically several similar approaches:
(1) To use such direct copies to the project;
(2) Build a class library project that contains this class, compile it as a DLL, and then add references in the app to use the SqlHelper in this Assembly;
(3) Register the DLLs in (2) as the GAC and use them directly in the application, as with System.IO and System.Net;
When the SqlHelper.cs is updated, you may feel very painful for (1) and (2), because you want to replace each application, and for (3) It is much simpler, just to re-register the compiled DLL to the GAC.
Remember seeing a word in MSDN once, saying yes. NET assembly solves the problem of DLL Hell ".
Say here, first tell a story: one day, a friend's colleague asked me, "Have you ever written a dynamic link library?" ", I unexpectedly did not think of what is, usually said DLL too much, almost all forget the" Dynamic link library "the words, but fortunately, there are still indelible memory.
“. NET assembly resolves the dll Hell "The key point is that the same name and different versions of the assembly can coexist, open the assembly directory, such as: C:\Windows\assembly, look more clearly."
Think about why multiple versions of the. NET Framework, Visual Studio, can be installed in the same system, and some software is not available, only a unique version, presumably.
2. Registering the DLL with the GAC
(1) Creating a DLL: Create a new Class library project with the name GACDemo, and then add a class such as:
Using System.Text;
Namespace GACDemo {public class Function {//Calculate string length, Chinese and full-width 1, other calculated 0.5 public static int Getstri Nglength (string text) {int Len = 0; Records non-Chinese, non-full-width characters for 0.5 lengths of float Charlen = 0; char[] chars = text. ToCharArray (); for (int i = 0; i < chars. Length; i++) {
int charlength = Encoding.UTF8.GetByteCount (Chars[i]. ToString ()); if (CharLength = = 3) { len++; } else { if (Charlen = = 0.5) {&NBSP;&NBSP;&Nbsp; Charlen = 0; } Else { Charlen = 0.5f; len++; } } } return Len; } }}
Right-click the project, select Properties from the menu, generate strong named key as follows (the assembly in the GAC must be Strong-name)
Then compile, before I register once, version 1.0.0.0, before compiling, I will modify the version in order to 2.0.0.0.
(2) Registration
To run the command line as an administrator
Go to the Bin/debug directory of the Class library project and execute gacutil-if gacdemo.dll
View C:\Windows\assembly
Two versions coexist.
3. Using the GAC in ASP.
This is simple, create a new ASP. NET site, add a reference, write the following test code
Using System; Using GACDemo;
namespace Gacwebdemo {public partial class _default:system.web.ui.page {protected void Page_Load ( Object sender, EventArgs e) {Response.Write (function.getstringlength ("Hello")); } } }
Compile the Web site.
To publish a Web site in IIS:
VS2008 directly right-click on the project publishing site.
VS2010 Right click on the website click Publish, select File System.
Post-Publish file directory:
Then publish the Web site in IIS (here is IIS7):
Then browse Default.aspx and see that the result of the output is 3.
It's important to note that. NET4.0 and the previous 2.0, 3.0, 3.5 install assemblies in the GAC are different:
because . NET 4.0 and . NET 2.0 use different CLR, there are now two GAC in Windows.
The GAC of the CLR 2.0 is still in C:\Windows\assembly\, while the GAC of the CLR 4.0 is in C:\Win Dows\microsoft.net\assembly.
Open CLR 4.0 The GAC, you will see three folders, GAC32, GAC64, and Gac_msil.
as the name implies, GAC32 is x86 platform,GAC64 is x (Note: Intel64 and AMD64 can all be called "AMD64"), and Gac_msil is available on both platforms ( when compiling any CPU).
Install the assembly into the GAC using the Gacutil tool: the. dll assembly is put into the Gac_msil directory , and the. exe assembly is placed in the GAC32 Directory
to be in Install the assembly in the GAC, first, or to strongly name the assembly. But the CLR 4.0 's GAC no longer supports drag-and-drop installations, so you can only use commands to install them honestly.
gacutil/i SharedAssembly.dll-- installation
GACUTIL/L sharedassembly-- View
gacutil/u sharedassembly-- Uninstall
Other references:
Installing assemblies in the GAC
How to install assemblies in the GAC
Global Assembly Cache
How to install assemblies to the Global assembly cache in Visual C #. NET
GAC of the [go] assembly---Global Assembly Cache