Improve C# Console App Startup and Runtimes with AOT and Avoid Spawning conhost.exe

I’ve written a very simple .NET8 C# app that does nothing other than fire off a HTTP request. None of the HTTP parameters need to ever change, and we don’t care about the return data or even whether the request is successful. All it does is make the request and then the program exits. The one important factor is startup and runtime, I’d like it to be as quick starting up and making the HTTP request as we can reasonably make it.

The first thing to do is enable Ahead Of Time (AOT) compilation. This compiles the IL (Intermediate Language) to native code at publish time so Just in Time compilation is not required, and as an added bonus our app can run on systems without the .NET runtime.

We can enable AOT with an addition to the csproj file:

<PropertyGroup>
    <PublishAot>true</PublishAot>
</PropertyGroup>

This reduced the runtime from around 200ms to ~90ms. Note: None of the timings in this post are particularly accurate, they were all taken from my dev machine with various things running in the background, but the relative differences between the values is what is important.

The next step was to get rid of the console window. As we’re not taking in any input or showing any output it was not necessary. Apart from being a visual annoyance it added a significant delay to our app.

This can be accomplished with another change to the csproj file, update the OutputType from Exe to WinExe:

<OutputType>WinExe</OutputType>

This change is a little unintuitive, it changes our Console app to a GUI app, but as there is no GUI in reality the only effect is the conhost.exe process is never spawned.

This last change reduced runtime to around 2ms, which is more than good enough for the use case we had.


If you enjoyed this post consider sharing it on , , , or , and .