GAC TIPS zz

GAC TIPS
http://blog.csdn.net/is2120/article/details/7050768
GAC是什么?
全局程序集缓存   (GAC)  
计算机范围内的代码缓存,它存储专门安装的程序集,这些程序集由计算机上的许多应用程序共享。在全局程序集缓存中部署的应用程序必须具有强名称
2011-12-7 6:04 PM IS2120@CSDN
是的,GAC中的所有的Assembly都会存放在系统目录"%winroot%\assembly下面。放在系统目录下的好处之一是可以让系统管理员通过用户权限来控制Assembly的访问。

关于GAC本身,上面redcaff_l所引述的一段话正是MSDN中对GAC的定义。GAC全称是Global   Assembly  Cache,他的作用是可以存放一些有很多程序都要用到的公共Assembly,例如System.Data、System.Windows.Forms等等。这样,很多程序就可以从GAC里面取得Assembly,而不需要再把所有要用到的Assembly都拷贝到应用程序的执行目录下面。举例而言,如果没有GAC,那么势必每个WinForm程序的目录下就都要从C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705下面拷贝一份System.Windows.Forms.dll,这样显然不如都从GAC里面取用方便,也有利于Assembly的升级和版本控制。

除了系统默认放置在GAC中的Assembly如System.Windows.Forms以外,我们也可以添加自己的Assembly:
1)创建一个strong-name的Assembly,例如ToolbarComponent.dll
2)运行gacutil   -i   ToolbarComponent.dll,把这个Assembly添加到GAC
3)在程序中动态装载:
System.Reflection.Assembly   ass=Assembly.Load( "ToolbarComponent,  Version=1.0.934.20434,   Culture=neutral,  PublicKeyToken=65f45658c8d4927f ");
MessageBox.Show( "Is   the   assembly   loaded   from  GAC?   "+ass.GlobalAssemblyCache);
在上面的程序中,ToolbarComponent就是从GAC装载而不是从程序的运行目录下的dll文件中装载,程序目录下不需要放置ToolbarComponent.dll程序也能正常运行。另外,Assembly.Load()中的参数可以通过 "gacutil   -l"查到。
2011-12-7 6:03 PM IS2120@CSDN
另外,上面提到了GAC中的Assembly必须是strong-name的。创建strong-name的Assembly的步骤大致如下:
a)   在命令行运行“sn   -k  keyPair.snk”创建一个密钥文件。这里的sn.exe也是.NET附带的一个工具。
b)   在VS.NET里面修改“AssemblyInfo.cs”文件:
[assembly:   AssemblyDelaySign(false)]  
[assembly:   AssemblyKeyFile( "..\\..\\keyPair.snk ")]  
c)   编译项目,就能得到一个strong-name的Assembly。

MSDN中有一些对GAC的介绍,您可以参考:
1)《Assembly   Cache   Viewer   (Shfusion.dll)》
2)《Global   Assembly   Cache》

.NET   Framework中附带了一些和GAC有关的工具,其中包括:
1)Gacutil.exe,一个命令行的工具,用于在GAC中浏览、添加、删除Assembly
2)Ngen.exe,也是一个命令行的工具,用于在GAC中创建Native   Image
3)mscorcfg.msc,一个MMC终端,可以图形化完成Gacutil.exe的主要功能。
2011-12-7 6:11 PM IS2120@CSDN
希望以上一些介绍能够对您有帮助。

Hogwarts   -   S(u)ddenly   dis@ppeared...

相关工具
自动注册assembly
You'll need:

  • Strong name your assembly (Visual Studio, Project Properties, Signing tab, Sign the assembly)
  • Alter your Build Events (Project Properties, Build Events tab, Post-build command line)

   cd C:\Program Files\Microsoft Visual Studio8\SDK\v2.0\Bin

   gacutil.exe /i "$(TargetPath)" /f/nologo

  gacutil /l "$(TargetName)" /nologo

Now, everytime you build yourproject, it'll be installed on GAC.

使用nirsoft的GACView工具
Try GACViewif you have a fear of command prompts.

直接拽入相关目录即可:
Just drag and drop the dll in C:\Windows\assembly using Windows Explorer

PowerShell:Install-Gac (GACUTIL for PowerShell)

   1:  BEGIN{

   2:      $ErrorActionPreference = "Stop"

   3:      

   4:      if ( $null -eq([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) ) {

   5:          [System.Reflection.Assembly]::Load("System.EnterpriseServices,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") |Out-Null

  6:      }

   7:      

   8:      $publish = New-ObjectSystem.EnterpriseServices.Internal.Publish

   9:  }

  10:  PROCESS{

  11:      $assembly = $null

  12:      

  13:      if ( $_ -is [string] ) {

  14:          $assembly = $_

  15:      } elseif ( $_ -is [System.IO.FileInfo] ) {

  16:          $assembly = $_.FullName

  17:      } else {

  18:          throw ("The object type '{0}' isnot supported." -f $_.GetType().FullName)

  19:      }

  20:      

  21:      if ( -not (Test-Path $assembly -type Leaf)) {

  22:          throw "The assembly '$assembly'does not exist."

  23:      }

  24:      

  25:      if ([System.Reflection.Assembly]::LoadFile( $assembly).GetName().GetPublicKey().Length -eq 0 ) {

  26:          throw "The assembly '$assembly' mustbe strongly signed."

  27:      }

  28:      

  29:      Write-Output "Installing:$assembly"

  30:      

  31:      $publish.GacInstall( $assembly )

  32:  }


使用编程的方法注册 gac ( dll assembly register 注册 )
相比以前使用fusion,.netframework 4 中提供了

Publish.GacInstall Method

.NET Framework 4

2011-12-7 6:04 PM IS2120@CSDN

Installs an assembly in the global assembly cache.

Namespace: System.EnterpriseServices.Internal
Assembly: System.EnterpriseServices (in System.EnterpriseServices.dll)

Syntax


public void GacInstall(
        string AssemblyPath
)

Parameters

AssemblyPath

Type: System.String
The file system path for the assembly.

Implements

IComSoapPublisher.GacInstall(String)

Exceptions


Exception

Condition

SecurityException

A caller in the call chain does not have permission to access unmanaged code.

Version Information


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET Framework Security


for the ability to access unmanaged code.Associated enumeration: SecurityPermissionFlag.UnmanagedCode

Platforms


Windows 7, Windows Vista SP1 or later,Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Corenot supported), Windows Server 2008 R2 (Server Core supported with SP1 orlater), Windows Server 2003 SP2

The.NET Framework does not support all versions of every platform. For a list ofthe supported versions, see.NET FrameworkSystem Requirements.

See Also http://blog.csdn.net/is2120/article/details/7050768


Reference

PublishClass

System.EnterpriseServices.InternalNamespace

http://blog.csdn.net/is2120/article/details/7050768 


原文地址:https://www.cnblogs.com/IS2120/p/6745973.html