5

I would like to reliably auto-install my application assemblies in the GAC when I compile my application using Visual Studio. I've setup pre- and post-build events in my web application's build.

Pre build event:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" /f /ul Uninstall.Gac.txt

Post build event:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" /f /il Install.Gac.txt
%windir%\system32\inetsrv\appcmd.exe recycle apppool -apppool.name:"Sharepoint IIS WebApps"

As you can see I'm also recycling my web application pool for the new assemblies to start working.

The problem I'm having is that this is a very unreliable process. In case my Web application fails, my next build will fail because uninstall will fail (post-build didn't run on previous build)... Etc.

I would like to make this process ass reliable as possible. I would like to conditionally uninstall and assemblies if they are present and forcibly install no matter whether they're there or not... But pre-build event is still important for the next reason:

Important

It's important that all my assemblies are completely uninstalled before build takes place, otherwise they're not copied to my output directory (since compiler finds them in GAC) hence GAC install fails since it can't find assemblies in the output \bin folder.

How should I reliably do GAC uninstallation and installation on my web application build?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • 1
    "Mater, what did I tell you about talking to the accused?" ["To not to."](http://www.sellsbrothers.com/Posts/Details/12503) – Marcelo Cantos Nov 01 '10 at 09:12
  • 1
    Why on earth do you want to do this on your dev machine and on every build? I can see having your install put something in the GAC, maybe, but the only reason to care on the dev box would be if these were for COM interop. Talk of recycling a web app pool suggests that's less likely. – Kate Gregory Nov 01 '10 at 18:19
  • 1
    Because I'm building a web app being run by sharepoint and I put my assemblies in the GAC so Sharepoint web app pool is able to access/run them. So when I make a change I reinstall them and recycle sharepoint app pool for the changes to take effect. – Robert Koritnik Nov 01 '10 at 19:49
  • Why are you specifying the /f option when uninstalling? – Mike Dour Nov 22 '10 at 17:10
  • @Mike Dour: To force uninstall in case it's being used. Check **gacutil** documentation – Robert Koritnik Nov 22 '10 at 17:24
  • If you want to have better control about install/uninstall just write your own version of gacutil using the GAC-API. See http://support.microsoft.com/default.aspx?scid=kb;en-us;317540 – TToni Nov 25 '10 at 15:50
  • You have not specified what kind of SharePoint solution you are working on, but there might be a way around GAC deployment while developing. Have you checked out http://msdn.microsoft.com/en-us/magazine/dd458798.aspx#id0090076 ? – Flo Nov 29 '10 at 13:43

2 Answers2

2

Instead onrelying on the GACUTIL tool, couldn't you program GAC install/uninstall? With code, you could at least rely on hresults error codes and react accordingly.

Information on the GAC API can be found here:

http://blogs.msdn.com/b/junfeng/archive/2004/09/14/229649.aspx

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

If the build succeeds, copy the output assemblies to another directory and install into GAC from there. And in pre-build use this directory to remove these assemblies from GAC.

This way there should never be any assemblies in GAC during compilation. You can tweak the post-build to copy the binaries only if all of them were successfully built by verifying in batch file if all output files EXIST.

Arunas
  • 918
  • 1
  • 8
  • 20
  • Does this matter? If compiler will find an assembly in the GAC it won't copy it to destination bin folder no matter from where the assembly was put into GAC. I see this as an additional unnecessary step. – Robert Koritnik Nov 26 '10 at 09:17
  • Well, I had to deal with these GAC issues because my app components were used by another program and had no problems by using the described method. I was passing assembly paths to gacutil by the way, so I needed for the assemblies to be present in given paths for gacutil to work. – Arunas Nov 26 '10 at 13:27