Visual Studio project templates without minimal API and top-level statement

Devs want the option to create new projects in Visual Studio without all of the minimalist stuff that was recently added (top-level statement, minimal API). At the very least, these features should be optional instead of being the default without a way to opt-out (to be fair, minimal API is optional). What I typically do … Read more

A fatal error occurred. The required library hostfxr.dll could not be found

Problem You are trying to run a .NET executable and you get the following error: A fatal error occurred. The required library hostfxr.dll could not be found.If this is a self-contained application, that library should exist in [C:\MyApp].If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the … Read more

C# – Where did Main() go?

When you create a new executable project in .NET 6, this is what you get (in a console app): A single line of code. The Main() method is missing. This is due to the top-level statement feature added in C# 9. The stated purpose of this feature is to “minimize the amount of code you … Read more

C# – Global exception event handlers

There are two global exception event available in all .NET applications: FirstChanceException: When any exception is thrown, this event is fired before anything else. UnhandledException: When there’s an unhandled exception, this event is fired right before the process is terminated. You wire up these event handlers in Main() (before anything else has executed), like this: … Read more

NLog – Log to console

There are two configuration options for logging to the console using NLog: Console target – uses the default text colors. ColoredConsole target – allows you to configure the color of logging messages. In this article, I’ll show how to configure these two targets using nlog.config. At the end, I’ll show an example of configuring NLog … Read more

C# – Parsing commands and arguments in a console app

In a console app there are two ways to get commands: Command line arguments passed into your program via Main(string[] args). User input from Console.ReadLine() (which you then split into a string[]). After getting a command, you have to parse it to figure out what code to execute. Typically commands have the following format: commandName … Read more

C# – Waiting for user input in a Console App

The following code shows how to wait for user input in a Console App: When the user types something in and presses the Enter key, Console.ReadLine() will return what they typed. Console.ReadLine() vs Console.ReadKey() Console.ReadLine() waits for the user to press Enter, and then returns everything they typed in. Console.ReadKey() returns individual key presses. It … Read more

C# – How to read custom configurations from appsettings.json

The appsettings.json file is a convenient way to store and retrieve your application’s configuration. You can add it to any project and then use the Microsoft.Extensions.Configuration library to work with it. Since appsettings.json is just a JSON file, you can add anything to it (as long as it’s valid JSON). Compared with working with the … Read more