.NET Runtime Optimization Service is missing

2.1k Views Asked by At

I am currently developing software which I would like while the software is installing, it can create the native images for the software in the background.

I am thinking of using NGEN and also set the process priority to low because I don't want it to consume 100% CPU. But I found out the ".NET Runtime Optimization Service" is actually missing from the "Services" on my computer. So NGEN won't actually continue all my process even when the computer is in "idle".

I just wonder is there any way I can re-register the service or are there any solutions for it?

2

There are 2 best solutions below

4
Walter Verhoeven On

Not sure what version of .net you are using, however ngen is not always doing the same on all Maschine's you would like to deploy to.

For that reason MS has altered the compiler and project structure. A small extract from my project shows this have a look at the PublishReadyToRun as this PublishTrimmed does what NGEN used to do, making a single file helps by setting PublishSingleFile to true would help if you like to deployment easy as it makes 1 huge exe and removes all dll's.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
  <ItemGroup>
    <TrimmerRootAssembly Include="mscorlib" />
    <TrimmerRootAssembly Include="System" />
    <TrimmerRootAssembly Include="System.IO" />
    <TrimmerRootAssembly Include="System.Threading.Tasks" />
  </ItemGroup>
…
</Project>

Have a look at Mr Hanselman blog on this.

One of the first things I noticed when I used it is that I had a FileNotFound exception when the exe loaded the dependencies. You can fix this by looking at the name of the file missing that basically got removed by the optimisation and add them to the TrimmerRootAssembly like I did in the sample. After adding those 4 I had no more issues.

NGEN Client machines that do not have NGEN or the "wrong" one would need to have the .NET Framework SDK installed (why would a user have a SDK/ Software Development Kit installed...)

if that file is missing (you can test for it using the path using something like this:

Directory.GetFiles($@"{Environment.GetFolderPath(Environment.SpecialFolder.Windows)}\Microsoft.NET\Framework\","ngen.exe",SearchOption.AllDirectories)

If that returns no file you can pop a message for the user to download and install the "right sdk" after that you can continue with your optimisation strategy using the command line for ngen.exe

LOW priority An easy trick to start your application in a lower thread priority is by starting it using the START command. enter image description here

10
Pavel Anikhouski On

I've checked the NGEN service on my Win10 machine and found only .NET Runtime Optimization Service v2.0.50727_X86 for old .NET 2.0/3.5, there is no service for .NET 4.x. But I found a couple of scheduled tasks in Task Scheduler Library/Microsoft/Windows/.NET Framework

enter image description here

This article and optimization script will help to figure the problem out. It seems, that for Win8 and later MS developers are using the task instead of service to run queued native image generation

var drainAppStoreQueue = function () {
        var schTasks = wsh.ExpandEnvironmentStrings("%windir%\\System32\\schtasks.exe");
        var arguments = "/run /Tn \"\\Microsoft\\Windows\\.NET Framework\\.NET Framework NGEN v4.0.30319";
        runToCompletion(schTasks, arguments + "\"", true);

        if (is64bit)
            runToCompletion(schTasks, arguments + " 64\"", true);
    }

    drainNGenQueue(isV4Installed ? "v4.0.30319" : "v2.0.50727");

    if (isOSWin8OrLater) {
        drainAppStoreQueue();
    }

I've tried to get a status of ngen service for both x86/x64 .NET Framework version and it reports

C:\Windows\Microsoft.NET\Framework\v4.0.30319>ngen queue status

Microsoft (R) CLR Native Image Generator - Version 4.8.3752.0 Copyright (c) Microsoft Corporation. All rights reserved.

Service name is: .NET Framework NGEN v4.0.30319 The .NET Runtime Optimization Service is stopped.

As I understood, it'll start when action is queued for the scheduled execution. According to ngen reference, you have a two options:

  1. Run ngen install [assemblyName | assemblyPath] to install the assemblies immediately and synchronously (and will take a time, but you run it directly)
  2. Or specify the /queue[:{1|2|3}], to make the image generation is scheduled (immediately or in idle time, depending on priority)

You can also test that tasks above are working by running ngen update /queue. Per documentation

If /queue is specified, the updates are queued for the native image service. Updates are always scheduled at priority 3, so they run when the computer is idle.

In my machine this command set the status of .NET Framework NGEN v4.0.30319 task to Queued and updates Last Run Time. You can always excute all scheduled actions synchronously by invoking ngen executeQueuedItems