A .NET console app (targeting .NET Framework 4.6.2) that uses configuration, dependency injection and logging as by default used in ASP.NET Core, using the separately available NuGet packages.
Setup
- Create solution in src directory
- Add .gitignore and .editorconfig
- Add src solution directory
- Add console app project (.NET Core) and adapt targeting to net462
- Show how an ASP.NET Core project looks
- Add App.config + transforms + update csproj for nesting
- Transforms are not used by default for App.config. Your CD system can use another way (e.g. replacing a string)
Configuration
- Add appsettings.json file + environment specific + update csproj for nesting and CopyToOutputDirectory
- Add a dummy
message
property to the appsettings
- Add a dummy
- Add reference to
System.Configuration
- Add following package references:
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
Startup configuration
- Create static
Startup
class- Create public method
void Configure()
- Create private method
IConfiguration SetupConfiguration()
- Implement SetupConfiguration method
- Temporarily
Console.WriteLine()
message property (insideConfigure()
method) - Call Startup in Program.Main()
- Run app
- Edit EnvironmentName and rerun (also with an unknown value) (Rebuild can be necessary!)
- Create public method
Dependency injection
- Using the default DI provider from Microsoft here, but others (like Autofac, StructureMap, ...) can of course be used as well
- Add following package reference:
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
- Add an
IApplication
interface and anApplication
class in theApp
folder- These are the main entry point of the app and will further resolve any other dependencies by themselves
- Add a method
void Run()
- Add an
IAppSettings
interface and anAppSettings
class in theApp
folder- They will match our specified properties in appsettings.json as a strongly typed model, and are injectable
- Add a property
string Message { get; }
- Also add a
set
in the class!
- Also add a
- Inject the IAppSettings in the constructor of Application + add property
- Write the AppSettings message in
Application.Run()
usingConsole.WriteLine()
- Remove the
Console.WriteLine()
in theStartup
class
- Write the AppSettings message in
Startup configuration
- Create private method
IServiceProvider ConfigureDependencyInjection(IConfiguration configuration)
- Create new
ServiceCollection
- Get the AppSettings from the Configuration object and register the instance as a singleton
- Register Application as a scoped service
- Return the built
IServiceProvider
object, and specify to validate scopes - Call the DI method in
Startup.Configure()
and return theIServiceProvider
object.
- Create new
- In
Program.Main()
, create a scope from theIServiceProvider
returned from theStartup.Configure()
method- Resolve the
IApplication
from the scope as aRequiredService
- Call the
Run()
method - Run the app (with different environment names) (Rebuild can be necessary!)
- Resolve the
Logging (if there's still enthusiasm :) )
- Using
Serilog
for logging implementation, but the abstractILogger<>
from Microsoft to call logging methods - Add following package reference:
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
- Configure the logger in the
Startup class
AddLogging()
on theServiceCollection
- Configure Serilog to
WriteTo.Console()
- Inject
ILogger<Application>
in the constructor of Application + add property- Replace
Console.WriteLine()
by a call on the logger
- Replace
- Run the app (with different environment names) (Rebuild can be necessary!)