Drag and drop a DLL to the GAC (“assembly”) in windows server 2008 .net 4.0

前端 未结 7 1346
清酒与你
清酒与你 2020-12-01 02:55

I\'ve trying to deploy some code to a client machine, where I don\'t want to install MS windows SDK tools. This means don\'t have access to \"gacutil\". I also have not c

7条回答
  •  有刺的猬
    2020-12-01 03:39

    The gacutil utility is not available on client machines, and the Window SDK license forbids redistributing it to your customers. When your customer can not, will not, (and really should not) download the 300MB Windows SDK as part of your application's install process.

    There is an officially supported API you (or your installer) can use to register an assembly in the global assembly cache. Microsoft's Windows Installer technology knows how to call this API for you. You would have to consult your MSI installer utility (e.g. WiX, InnoSetup) for their own syntax of how to indicate you want an assembly to be registered in the Global Assembly Cache.

    But MSI, and gacutil, are doing nothing special. They simply call the same API you can call yourself. For documentation on how to register an assembly through code, see:

    KB317540: DOC: Global Assembly Cache (GAC) APIs Are Not Documented in the .NET Framework Software Development Kit (SDK) Documentation

    var IAssemblyCache assemblyCache;
    CreateAssemblyCache(ref assemblyCache, 0);
    
    
    String manifestPath = "D:\Program Files\Contoso\Frobber\Grob.dll";
    
    FUSION_INSTALL_REFERENCE refData;
    refData.cbSize = SizeOf(refData); //The size of the structure in bytes
    refData.dwFlags = 0; //Reserved, must be zero
    refData.guidScheme = FUSION_REFCOUNT_FILEPATH_GUID; //The assembly is referenced by an application that is represented by a file in the file system. The szIdentifier field is the path to this file.
    refData.szIdentifier = "D:\Program Files\Contoso\Frobber\SuperGrob.exe"; //A unique string that identifies the application that installed the assembly
    refData.szNonCannonicalData = "Super cool grobber 9000"; //A string that is only understood by the entity that adds the reference. The GAC only stores this string
    
    //Add a new assembly to the GAC. 
    //The assembly must be persisted in the file system and is copied to the GAC.
    assemblyCache.InstallAssembly(
          IASSEMBLYCACHE_INSTALL_FLAG_FORCE_REFRESH, //The files of an existing assembly are overwritten regardless of their version number
          manifestPath, //A string pointing to the dynamic-linked library (DLL) that contains the assembly manifest. Other assembly files must reside in the same directory as the DLL that contains the assembly manifest.
          refData);
    

    More documentation before the KB article is deleted:

    The fields of the structure are defined as follows:

    • cbSize - The size of the structure in bytes.
    • dwFlags - Reserved, must be zero.
    • guidScheme - The entity that adds the reference.
    • szIdentifier - A unique string that identifies the application that installed the assembly.
    • szNonCannonicalData - A string that is only understood by the entity that adds the reference. The GAC only stores this string.

    Possible values for the guidScheme field can be one of the following:

    FUSION_REFCOUNT_MSI_GUID - The assembly is referenced by an application that has been installed by using Windows Installer. The szIdentifier field is set to MSI, and szNonCannonicalData is set to Windows Installer. This scheme must only be used by Windows Installer itself. FUSION_REFCOUNT_UNINSTALL_SUBKEY_GUID - The assembly is referenced by an application that appears in Add/Remove Programs. The szIdentifier field is the token that is used to register the application with Add/Remove programs. FUSION_REFCOUNT_FILEPATH_GUID - The assembly is referenced by an application that is represented by a file in the file system. The szIdentifier field is the path to this file. FUSION_REFCOUNT_OPAQUE_STRING_GUID - The assembly is referenced by an application that is only represented by an opaque string. The szIdentifier is this opaque string. The GAC does not perform existence checking for opaque references when you remove this.

提交回复
热议问题