This is a simple .NET program showing code to set up to get basic DI / Logging / Options configuration in a .NET application.
Last active
October 16, 2019 17:55
-
-
Save jerhon/c5c4b2a6740270cd2fd145f52a3c57ab to your computer and use it in GitHub Desktop.
.NET Core CommandLine Program (Simple Template)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
namespace SampleApp { | |
class Program { | |
static void Main(string[] args) { | |
var builder = new HostBuilder() | |
// Setup configuration of the application | |
.ConfigureAppConfiguration((appConfig) => { | |
appConfig.Sources.Clear(); | |
appConfig.SetBasePath(Path.Join(Directory.GetCurrentDirectory(), "config")) | |
.AddJsonFile("appsettings.json") | |
.AddJsonFile("appsettings.private.json"); | |
}) | |
.ConfigureLogging((hbc, logger) => { | |
logger.AddConsole(); | |
logger.AddConfiguration(hbc.Configuration.GetSection("Logging")); | |
}) | |
.ConfigureServices((ctx, services) => { | |
services.AddOptions() | |
.AddScheduler() | |
.AddTransient<SERIVCE_HERE>() | |
.Configure<OPTIONS_CLASS_HERE>(ctx.Configuration) | |
}) | |
.UseConsoleLifetime(); | |
var host = builder.Build(); | |
// Example configuring the services collection | |
host.Services.UseScheduler(scheduler => { | |
scheduler | |
.Schedule<SOME_SCHEDULE_SERVICE>() | |
.EveryThirtySeconds() | |
.PreventOverlapping("HotFolder"); | |
}); | |
using (host) { | |
await host.StartAsync(); | |
await host.WaitForShutdownAsync(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment