Using WiX to run NGen 2.0 against your application during installation
Friday, October 7th, 2005 @ 11:03 am | Development
I wanted to run NGen 2.0 on my application (here’s why) as part of the MSI
database generated from a WiX script and run by Windows Installer. I found a
couple of
posts from Robert Pickering that
pointed me in the right direction, but stopped short of the copy & paste
fragment of XML I was looking for. I did, however, figure out how to do it and here is
my solution:
<CustomAction Id="MyApp.exeNGenInstall" ExeCommand='"[WindowsFolder]Microsoft.NET\Framework\v2.0.50215\ngen.exe" install "[#MyApp.exe]" /queue:2 /nologo /silent' Directory="INSTALLDIR" Return="asyncNoWait" /> <CustomAction Id="MyApp.exeNGenUninstall" ExeCommand='"[WindowsFolder]Microsoft.NET\Framework\v2.0.50215\ngen.exe" uninstall "[#MyApp.exe]" /nologo /silent' Directory="INSTALLDIR" Return="ignore" /> <InstallExecuteSequence> <Custom Action="MyApp.exeNGenInstall" After="InstallFinalize"> NOT Installed </Custom> <Custom Action="MyApp.exeNGenUninstall" Before="RemoveFiles"> Installed </Custom> </InstallExecuteSequence>
Bung the above into the <Product> element of your .wxs
file, change the references to MyApp.exe as appropriate and you’re
probably done. If you’re not using NET 2.0 Beta 2 you will also have to alter
the path to ngen.exe to match your particular framework version.
Note that NGen 2.0 will automatically work out what it needs to do given the
top-level assembly in your application, which simplifies matters a great deal; I
doubt if it’s that simple with previous versions of the .NET framework…
The only problem I am aware of is that the custom action opens a command
prompt window briefly while NGen is run. If anyone knows how to tidy up this
loose end then please do let me know!
Update: I’ve got it working without the command prompt windows. Thanks, Rob!

To hide the command prompt displayed, take a look at the “Quiet Execution CustomAction” topic in the WiX.chm. The QtExec CustomAction provided with the WiX toolset does what you want.