Skip to content

marceln/net-generic-host-boilerplate

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

net-generic-host-boilerplate

This is a .NET 6 boilerplate for generic host console apps.

If you're like me and frequently in need of creating long running .NET 6 console apps, then you can use this. It's especially good if your structure and implementation grows over time.

It covers the basic necessities of a generic host console app:

  • Logging, via Microsoft.Extensions.Logging. You can plug your own if you want.
  • Dependency injection.
  • Async/await processing inside the BackgroundService instance.
  • Configuration, through environment vars and/or appsettings.json.

Installation

Nothing fancy, just run:

git clone git@github.com:marceln/net-generic-host-boilerplate.git

Then you can change the solution and project names.

Running

You can run this from Visual Studio 2022 or through the .NET CLI but don't forget first to set your DOTNET_ENVIRONMENT environment variable.

In Visual Studio 2022:

  • Project properties -> Debug -> Open debug launch profiles UI
  • Edit launchSettings.json:
{
  "profiles": {
    "GenericHostBoilerplate.App": {
      "commandName": "Project",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    }
  }
}

The latter also works for Visual Studio Code.

Implementation

The boilerplate provides a BackgroundService implementation as well as an ExampleService so it can demonstrate dependency injection. In the BackgroundHostedService class you can also see how you can use some other framework-provided services, like IConfiguration, ILogger<>, IHostApplicationLifetime and IHostEnvironment.

Have fun with it!

About

A .NET 6 boilerplate for generic host console apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages