Console - Nano-Core/Nano.Library GitHub Wiki
Table of Contents
Summary
The ConsoleApplication
derives from DefaultApplication
. It initializes the required services and into a service collection, and is able to handle and resolve dependencies, similar to a web application. This is explained in details further down.
Use the ConsoleApplication
when you require to construct a background-worker, cloud function or similar single executing application.
Sample implementation
public class Program
{
public static void Main()
{
ConsoleApplication
.ConfigureApp()
.ConfigureServices(x =>
{
// Additional dependencies...
})
.Build()
.Run();
}
}
Configuration
The Console
section of the configuration defines behavior related to the application. The section is deserialized into an instance of ConsoleOptions
, and injected as dependency during startup, thus available for injection throughout the application.
See Appendix - App Settings for details about the console section and the meaning of the variables.
Console Section
"Console":
{
}
Worker Process
When the console application is run, a background-worker is started and executed until finished, and the application shuts down.
The abstract BaseWorker
implements IHostedService
, and is the top-most worker implementation. The concrete DefaultWorker
implementation derives from BaseWorker
, and contains default method implementations of StartAsync
and StopAsync
. Derive your own worker implementation from DefaultWorker
, as shown below.
Sample implementation
public class MyWorker : DefaultWorker
{
public MyWorker(ILogger logger, IRepository repository, IEventing eventing, IApplicationLifetime applicationLifetime)
: base(logger, repository, eventing, applicationLifetime)
{
}
public override async Task StartAsync(CancellationToken cancellationToken = default)
{
await base.StartAsync(cancellationToken);
// Custom implementation
}
public override async Task StopAsync(CancellationToken cancellationToken = default)
{
// Custom implementation
await base.StopAsync(cancellationToken); // Stops application
}
}
Injected Services
When building the ConsoleHost
, dependencies related to hosting is configured and initialized. This evolves around defining the process and environment in which the application is executing in.
Dependencies:
Nano.Console.ConsoleOptions
Nano.Console.ConsoleApplication
Nano.Console.Workers.BaseWorker
For a full list of services and dependencies, see Injected Dependencies