views:

1979

answers:

8

There seems to be lots of different ways to register assemblies with the GAC, as in, they 'work'. However, what's the "proper" way of doing it?

In response to Lou Franco (and gacutil):

I'm using Gacutil for development, but it seems to me to be not the proper way to install it, since gacutil isn't included in the basic .NET utilities past .NET 1.1 - it's only a developer tool.

Additional: Gacutil (as seen in responses below) is not redistributable, and therefore should not be used in any app that you intend to give to people who are not developers. AKA, customers. See This blog post (and comments) by Aaron Stebner.

In response to using WIX:

WIX might be great and all, but how does it work under the hood? What details makes the way WIX installs the assembly the right way to install it? How does it look it up? Is it a system/.NET call? Is there some call in a dll buried somewhere in System32 that needs to be made?

(Edit: it looks like WIX uses MSI under the hood. See my comments in the accepted answer.)

Final edit: It looks like the correct way to install an assembly to the GAC is using windows installer, and nothing else. I'm going to give Wix a try. Thanks all!

A: 

use gacutil.

Advantages: seems to always work. Disadvantages:

  • must package an additional executable with your installer.
  • As a development utility, seems to have additional side effects (like forcing the install no matter what).
  • Should NOT be included in any redistributable given to customers.
Robert P
A: 

Doesn't your installer maker have a way of installing an assembly into the GAC? On your own, I'd say GACUTIL:

http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx

Lou Franco
Yes, you can create an installer that puts DLLs in the GAC
Will
Installers will either add the file directly to the msiAssembly and msiAssemblyName tables with appropriate values OR use a snapshot differencing method to determine the exact locations of the files and the required registry entries.
Anonymous Type
A: 

copy directly to %WINDIR%\Assembly.

Advantage: Straightforward.

Disadvantage: AFAIK, %WINDIR%\Assembly just happens to be where it is right now, and it's location is subject to change. This would make it break in future versions of windows or if that folder's behavior chaneges. This probably isn't the right way.

Extreme Disadvantage: As said by madmath:

just copying your assembly into c:\windows\assembly won't work. Explorer only shows a simplified view of the folder, which contains in fact lots of different folders for different kinds of assemblies. Doing a copy in it from an installer won't trigger all the operations done by explorer on a drag-and-drop. (written here because I don't have enough reputation yet to comment on other posts).
Robert P
Agreed -- I don't want to downvote -- but you shouldn't do this.
Lou Franco
Agreed. Throwing it out there to make sure it's covered, and people know it's bad. :)
Robert P
A: 

If you don't want to handle the gacutil stuff yourself, you can always create a setup project in visual studio.

But I'd stick with gacutil myself.

Dana
+2  A: 

Use System.EnterpriseServices.Internal.Publish's GacInstall method.

Advantages: Seems to be an internal tool. Probably does all the right stuff.

Disadvantages: As part of an installer, you'd still need to make and run an app that calls this (unless the installer you make is a custom app that does it anyway).

Robert P
A: 

The best way is to use gacutil -i Library.dll.

The only problem with gacutil is that it is not in the default PATH of the system. It is however in a fixed location relative to the windows directory, for a given .Net Framework version. So you can use the following command line to execute it from anywhere:

%SystemRoot%\Microsoft.Net\Framework\v1.1.4322\gacutil -i

PS: just copying your assembly into c:\windows\assembly won't work. Explorer only shows a simplified view of the folder, which contains in fact lots of different folders for different kinds of assemblies. Doing a copy in it from an installer won't trigger all the operations done by explorer on a drag-and-drop. (written here because I don't have enough reputation yet to comment on other posts).

Mathieu Garstecki
+13  A: 

With Wix I would do something like this:


<DirectoryRef Id="MyDirectory" >
    <Component Id="MyComponent" Guid="PUT-GUID-HERE" DiskId="1">
        <File Id="MyAssembly" Name="MyAssembly.dll" Assembly=".net" KeyPath="yes" Source="MyAssembly.dll" />
    </Component>
</DirectoryRef>

When you use the attribute Assembly=".net" for a file in WiX, it will create entries in the MsiAssembly and MsiAssemblyName table for this component and mark it as a GAC component.

CheGueVerra
MSI (which wix uses) seems to be the solution. AFAIK (with research and ideas given in this question) there is no other proper way of installing an assembly to the GAC currently available in windows.
Robert P
Here's a link to a WIX tutorial http://www.tramontana.co.hu/wix/
CheGueVerra
Beware, in major upgrade scenarios, the assembly will disappear from GAC in some cases: http://support.microsoft.com/kb/905238
koltun
What is `MyDirectory`?
stacker
+2  A: 

http://blogs.msdn.com/astebner/archive/2006/11/04/why-to-not-use-gacutil-exe-in-an-application-setup.aspx

It looks like the gacutil should be avoided; it's not a redistributable app. Instead, the 'proper' way of installing them seems to be using MSI, one way being WIX, as posted by CheGueVerra or another script.

Robert P