Skip to content

Instantly share code, notes, and snippets.

@jerhon
Last active October 16, 2019 17:55
Show Gist options
  • Save jerhon/c5c4b2a6740270cd2fd145f52a3c57ab to your computer and use it in GitHub Desktop.
Save jerhon/c5c4b2a6740270cd2fd145f52a3c57ab to your computer and use it in GitHub Desktop.
.NET Core CommandLine Program (Simple Template)

Overview

This is a simple .NET program showing code to set up to get basic DI / Logging / Options configuration in a .NET application.

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