Start faster programs installed by using Ngen.exe
environment
- Visual Studio
-
- Visual Studio Community 2017
- Visual Studio Community 2019
- WiX Toolset
- 3.11.2
※ It works in other versions, but it is unconfirmed
At first
The .NET Framework application creates and installs native images by using Ngen.exe. You can speed up startup and execution.
However, in order to create a native image, you must use Ngen.exe in the environment where you run the application. You cannot distribute a pre-imaged image. Therefore, do you want users to run Ngen.exe manually? It must be run during installation.
This section describes the steps to perform Ngen.exe on the target exe or dll in wiX settings during installation.
Note that Ngen.exe is only valid for .NET Framework applications and is originally native assembly. There is no point in using it for .NET Core applications.
Preparation in advance
- Assume that you have created an installer with WiX.
Add library reference
Add WixNetFxExtension.dll to your reference. The folder path is C:\Program Files (x86)\WiX Toolset v3.11\bin.
Edit Product.wxs
Add settings to files that run 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">
Adding parameters to components (files) that create native images
Add 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 exe or dll refer to (depends) on other dlls due to the nature of Ngen.exe, you can specify only the exe or dll from which you want to refer to it. For example, if A.exe refers to B.dll, setting NativeImage to A.exe only automatically applies to B.dll.
However, if A.exe dynamically loads B.dll using Assembly.Load and the like after execution, B.dll is not a dependency. Using Ngen.exe for A.exe does not compile the B.dll. In this case, set nativeImage to B.dll as well.
@Id
Identification name of the native image. Do not overlap other IDs.
@Platform
Specify either "32bit", "64bit" or "all". Change the settings at 32bit or 64bit for the application you are installing. All creates both native images, so specify libraries that switch according to the execution state such as "Any CPU".
@Priority
When to run Ngen.exe during installation. You can specify 0-3, but for more information, please refer to the official website.
Summary
After you create the installer, you can see that if the installation succeeds, a native image is created and registered in the assembly folder.