ConnyConsole is a console CLI project that uses System.CommandLine
from Microsoft for argument parsing to collect some experience with this library.
This chapter gives a rough overview what was implemented as example implementation.
Please note that some the features listed are based on or inspired by the article series A Beginner's Guide to .NET's HostBuilder: Part 1 and following.
- Console startup implemented with Host.CreateDefaultBuilder that enables/contains following:
- Dependency injection configuration via
ConfigureServices
and extension method to keepProgram.cs
simple;- Own extension method
AddConfiguration
registers all dependencies incl.:- Logger configuration with injected configuration;
- Configuration registered for options pattern;
- Own extension method
- Loads configuration from specific subdirectory
Config
that containsappsettings.json
andappsettings.Development.json
:- Current environment resolved from injected
HostBuilderContext
to use relatedappsettings.json
file; appsettings
files intentionally in subdirectoryConfig
to have to define them specifically for loading, to cover case as example;
- Current environment resolved from injected
- Dependency injection configuration via
- Serilog set up as logger:
- Startup-logger and injectable logger based on configuration files;
- Logging into on console and file in defined format;
- Serilog can throw strange/not relatable exceptions during configuration, when JSON config is wrong, such exceptions will be printed on console;
- Current dummy logic executed async with graceful and enforceable cancellation;
- First
[Ctrl] + [C]
or[Ctrl] + [Break]
initiates graceful cancellation;- Waits until logic finishes or configurable timeout reached and closes application;
- Second
[Ctrl] + [C]
or[Ctrl] + [Break]
initiates immediate enforced cancellation;- Application exists immediately;
- All that magic happens in
ConsoleCancellationTokenSource
class; - Console cancellation event is registered in
App
class;
- First
- Console application icon defined (check
*.csproj
file);
As an application is only as good as it was tested, this chapter gives some insights how the console application tests were implemented.
- Unit tests implemented with following libraries/frameworks:
- AutoFixture.AutoNSubstitute
- AwesomeAssertions (fully community driven fork of FluentAssertions)
- NSubstitute
- xUnit
- Graceful + enforced cancellation are tested with simulated
[Ctrl] + [C]
keys pressed (ConsoleCancellationTokenSourceTests.cs
); - Dependency injection extension method incl. lifetime checks;
- Lifetime check helps to notice fast if a lifetime was changed by accident or just to highlight that it was changed in general;
- Async dummy logic execution;
This chapter provides an overview what the current build pipeline provides.
- GitVersion integrated for auto SemVer (Semantic Versioning) based on Git history;
- SonarQube (cloud, free plan) integrated;
- Build console application with
Release
configuration; - Run tests;
- Collect test run results as
*.trx
file; - Collect code coverage in
OpenCover
format, later on used to publish on SonarQube;- Done by using coverlet.msbuild + coverlet.collector (in test project only);
- Both files, coverage file and test result file, are published as build artifacts;
- Passed, failed and skipped tests listed as part of run summary, realized with
GitHubActionsTestLogger
package;
- Collect test run results as
Following are some used articles listed.