Faster startup of programs installed using Ngen.exe

Page updated :

Environment

Visual Studio
  • Visual Studio Community 2017
  • Visual Studio Community 2019
WiX Toolset
3.11.2

※ Works in other versions, but is unconfirmed

At first

The .NET Framework application creates and installs native images by using Ngen.exe. You can speed up the start and run speed.

However, to create a native image, you must use Ngen.exe in the environment in which you are running the application. You cannot distribute images of yourself in advance. Therefore, do you want the user to run Ngen.exe manually? It must be run during installation.

This section provides step-by-step instructions on how to run Ngen.exe during installation on the target exe or dll in wix settings.

Note that Ngen.exe is valid only for .NET Framework applications and is originally a native assembly or There is no point in using .NET Core applications.

Pre-preparation

  • Assume that you have created an installer in WiX.

Add library references

Add WixNetFxExtension.dll to the reference. The folder path is C:\Program Files (x86)\WiX Toolset v3.11\bin.

image

image

image

Edit Product.wxs

Add settings to the file that runs Ngen.exe.

Wix/@xmlns

Add the NetFxExtension namespace.

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
		 xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">

Add parameters to the component (file) that creates the native image

Adds netfx:NativeImage to the child elements of the target file.

<Component Win64="yes" Id="OGGVORBISPROJECT.DLL" DiskId="1" Guid="6A1D2FD8-9FA7-40D5-A0EA-356268B3C0B4">
  <File Id="OGGVORBISPROJECT.DLL" Name="OggVorbisProject.dll" Source="LittleSaviorTrial\Dll\OggVorbisProject.dll">
    <netfx:NativeImage Id="NGEN_OGGVORBISPROJECT.DLL" Platform="64bit" Priority="0"/>
  </File>
</Component>

If the nature of Ngen.exe and the exe or dll are referring (depends) to other dlls, it is OK to specify only the source exe or dll. For example, if A.exe refers to B.dll, setting NativeImage only to A.exe automatically targets B.dll.

However, if A.exe loads B.dll dynamically using Assembly.Load, etc. after execution, B.dll is not a dependency. Using Ngen.exe for A.exe does not compile B.dll. In this case, set The NativeImage to B.dll as well.

@Id

The distinguished name of the native image. Do not overlap with other IDs.

@Platform

Specify 32bit, 64bit, or all. Change the settings to 32bit or 64bit of the application you want to install. If all creates both native images, specify libraries that switch depending on the execution state, such as "Any CPU".

@Priority

It is time to run Ngen.exe during installation. You can specify 0-3, but for more information, please visit the official website.

Summary

After you create the installer, you can see that if the installation is successful, a native image is created and registered in the assembly folder.

image