71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Windows;
|
|
using Serilog;
|
|
using System;
|
|
using WpfApp1TRC20;
|
|
using WpfApp1TRC20.Views;
|
|
using WpfApp1TRC20.Data;
|
|
using WpfApp1TRC20.Services;
|
|
using WpfApp1TRC20.ViewModels;
|
|
|
|
namespace WpfApp1TRC20
|
|
{
|
|
public partial class App : Application
|
|
{
|
|
private IHost _host;
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
{
|
|
// Configure Serilog
|
|
Log.Logger = new LoggerConfiguration()
|
|
.WriteTo.File("logs/app-.txt", rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
_host = Host.CreateDefaultBuilder()
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
// Database
|
|
services.AddDbContext<AppDbContext>();
|
|
|
|
// Services
|
|
services.AddSingleton<ITronService, TronService>();
|
|
services.AddScoped<IDataService, DataService>();
|
|
|
|
// ViewModels
|
|
services.AddTransient<MainViewModel>();
|
|
|
|
// Views
|
|
services.AddTransient<MainWindow>();
|
|
|
|
// Logging
|
|
services.AddLogging(a =>
|
|
a.AddProvider( ));
|
|
})
|
|
.Build();
|
|
|
|
await _host.StartAsync();
|
|
|
|
// Ensure database is created
|
|
using (var scope = _host.Services.CreateScope())
|
|
{
|
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
await context.Database.EnsureCreatedAsync();
|
|
}
|
|
|
|
var mainWindow = _host.Services.GetRequiredService<MainWindow>();
|
|
mainWindow.Show();
|
|
|
|
base.OnStartup(e);
|
|
}
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
{
|
|
await _host?.StopAsync();
|
|
_host?.Dispose();
|
|
Log.CloseAndFlush();
|
|
base.OnExit(e);
|
|
}
|
|
}
|
|
} |