static void Main(string[] args) { // Configuration var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); var configuration = builder.Build(); // Database var optionsBuilder = new DbContextOptionsBuilder <SqliteConsoleContext>() .UseSqlite(configuration.GetConnectionString("DefaultConnection")); var context = new SqliteConsoleContext(optionsBuilder.Options); context.Database.EnsureCreated(); // Services var services = new ServiceCollection() .AddLogging() .AddSingleton(configuration) .AddSingleton(optionsBuilder.Options) .AddSingleton <IExampleService, ExampleService>() .AddDbContextPool <SqliteConsoleContext>(options => options.UseSqlite(configuration.GetConnectionString("DefaultConnection"))) .BuildServiceProvider(); // Logging services .GetService <ILoggerFactory>() .AddConsole(LogLevel.Trace); var logger = services.GetService <ILoggerFactory>() .CreateLogger <Program>(); logger.LogInformation($"Starting application at: {DateTime.Now}"); // Example Service var service = services.GetService <IExampleService>(); service.AddExample("Test A"); service.AddExample("Test B"); service.AddExample("Test C"); service.GetExamples(); }
public ExampleService(ILoggerFactory loggerFactory, IConfigurationRoot configurationRoot, SqliteConsoleContext sqliteConsoleContext) { logger = loggerFactory.CreateLogger <ExampleService>(); config = configurationRoot; context = sqliteConsoleContext; }