I’m a lost helicopter pilot

I’ve been having a joy developing in .NET 6.

I’m only half joking. Some of the capabilities in the ecosystem are simply wonderful… once you figure them out.

In the process, most of the reference documentation is absolutely useless.

Consider my latest adventure. I just wanted to play with some .NET “dependency injection” in the console-app project I had created for playing with things. I found this article, in the “.NET fundamentals” docs, all about the “.NET Generic Host.”

Excellent. That sounds like what I want.

That article contains a snippet of code describing how to set up the host builder, a list of framework-provided services (which should be listed in the reference documentation but isn’t), sections on how to configure the host and how to configure the app—and I don’t know what the difference is between those two, and the piece didn’t explain it; all it said was, “Host configuration is used to configure properties of the IHostEnvironment implementation,” and “App configuration is created by calling ConfigureAppConfiguration on IHostBuilder.” It also has a section on how to shutdown the host, including a detailed sequence diagram of the shutdown sequence.

What they didn’t explain was where do I hook in my custom code so that it will run?

The reference documentation contains pearls like:

  • Host.CreateDefaultBuilder – “Initializes a new instance of the HostBuilder class with preconfigured defaults.” Okay… I guess that’s good to know? The reference documentation never explains what the defaults are or how to override them. You need to search elsewhere for that information.
  • HostBuilder Class – Implements IHostBuilder. Wow. Didn’t see that coming.
  • IHostBuilder Interface – A program initialization abstraction. Now you’re just making fun of me, right?
  • IHost.Run – Runs an application and blocks the calling thread until host shutdown is triggered and all IHostedService instances are stopped.

Well, I’m glad they explained that to me. That really cleared things up.

I swear, it’s that old joke:

Lost helicopter pilot: Where am I?

Microsoft employee: You’re in a helicopter.

This is par for the course, and I’ve never experienced it to this degree, not with Perl, not with Python, not with Java, not with JavaScript, not with GoLang. I never remember the old Windows docs of 15 years ago being this bad.

The “Generic Host” article does have a line of sample code that says:


…but nowhere do they explain what this Worker class is.

There is also an article on “Worker Services in .NET,” but that appears to be something completely different.

Or is it?…

After some head-scratching and some hit-and-miss exploration, I speculated that maybe what I needed to do was to create a class that derives from BackgroundService and implements the ExecuteAsync() method—Thank you, Jetbrains Rider!—then add that class as a hosted service using AddHostedService as above.

You know, someone who actually knew what he was doing might’ve simply said so.

This is the umpteenth time I’ve gone through this kind of thing in the past month.

Figuring out how to use the current .NET APIs is like playing an old-time adventure puzzle game. Find the key that fits the lock, lots of pixel-hunting, and you’re in a maze of twisty little passages, all alike.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a reply