The following command generates a native image for ClientApp.exe, located in the current directory, and installs the image in the native image cache. If a configuration file exists for the assembly, Ngen.exe uses it. In addition, native images are generated for any .dll files that ClientApp.exe references.
ngen install ClientApp.exe
An image installed with Ngen.exe is also called a root. A root can be an application or a shared component.
The following command generates a native image for MyAssembly.exe with the specified path.
ngen install c:\myfiles\MyAssembly.exe
When locating assemblies and their dependencies, Ngen.exe uses the same probing logic used by the common language runtime. By default, the directory that contains ClientApp.exe is used as the application base directory, and all assembly probing begins in this directory. You can override this behavior by using the /AppBase option.
Note |
|---|
| This is a change from Ngen.exe behavior in the .NET Framework versions 1.0 and 1.1, where the application base is set to the current directory. |
An assembly can have a dependency without a reference, for example if it loads a .dll file by using the System.Reflection.Assembly.Load method. You can create a native image for such a .dll file by using configuration information for the application assembly, with the /ExeConfig option. The following command generates a native image for MyLib.dll, using the configuration information from MyApp.exe.
ngen install c:\myfiles\MyLib.dll /ExeConfig:c:\myapps\MyApp.exe
Assemblies installed in this way are not removed when the application is removed.
To uninstall a dependency, use the same command-line options that were used to install it. The following command uninstalls the MyLib.dll from the previous example.
ngen uninstall c:\myfiles\MyLib.dll /ExeConfig:c:\myapps\MyApp.exe
To create a native image for an assembly in the global assembly cache, use the display name of the assembly. For example:
ngen install "ClientApp, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL"
NGen.exe generates a separate set of images for each scenario you install. For example, the following commands install a complete set of native images for normal operation, another complete set for debugging, and a third for profiling:
ngen install MyApp.exe
ngen install MyApp.exe /debug
ngen install MyApp.exe /profile
Displaying the Native Image Cache
Once native images are installed in the cache, they can be displayed using Ngen.exe. The following command displays all native images in the native image cache.
The display action lists all the root assemblies first, followed by a list of all the native images on the computer.
Use the simple name of an assembly to display information only for that assembly. The following command displays all native images in the native image cache that match the partial name MyAssembly, their dependencies, and all roots that have a dependency on MyAssembly:
Knowing what roots depend on a shared component assembly is useful in gauging the impact of an update action after the shared component is upgraded.
If you specify an assembly's file extension, you must either specify the path or execute Ngen.exe from the directory containing the assembly:
ngen display c:\myApps\MyAssembly.exe
The following command displays all native images in the native image cache with the name MyAssembly and the version 1.0.0.0.
ngen display "myAssembly, version=1.0.0.0"
Updating Images
Images are typically updated after a shared component has been upgraded. To update all native images that have changed, or whose dependencies have changed, use the update action with no arguments.
Updating all images can be a lengthy process. You can queue the updates for execution by the native image service by using the /queue option. For more information on the /queue option and installation priorities, see Native Image Service.
Uninstalling Images
Ngen.exe maintains a list of dependencies, so that shared components are removed only when all assemblies that depend on them have been removed. In addition, a shared component is not removed if it has been installed as a root.
The following command uninstalls all scenarios for the root ClientApp.exe:
The uninstall action can be used to remove specific scenarios. The following command uninstalls all debug scenarios for ClientApp.exe:
ngen uninstall ClientApp /debug
Note |
|---|
| Uninstalling /debug scenarios does not uninstall a scenario that includes both /profile and /debug. |
The following command uninstalls all scenarios for a specific version of ClientApp.exe:
ngen uninstall "ClientApp, Version=1.0.0.0"
The following commands uninstall all scenarios for "ClientApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL", or just the debug scenario for that assembly:
ngen uninstall "ClientApp, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL"
ngen uninstall "ClientApp, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL" /debug
As with the install action, supplying an extension requires either executing Ngen.exe from the directory containing the assembly or specifying a full path.
For examples relating to the native image service, see Native Image Service.