Superlocal.Agile/src/ui/Superlocal.Agile.WinApp/Program.cs

67 lines
2.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Superlocal.Agile.Infrastructure.Database;
using Superlocal.Agile.WinApp.Models;
namespace Superlocal.Agile.WinApp
{
internal static class Program
{
#region Properties
public static Settings Settings { get; private set; }
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
Settings = config.GetRequiredSection("Settings").Get<Settings>();
var dbPath = Settings.DbPath;
if (string.IsNullOrWhiteSpace(Path.GetDirectoryName(dbPath)))
{
dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Superlocal.Agile", dbPath);
}
var connectionString = string.Format("Data Source={0};", dbPath);
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<MainForm>()
.AddLogging(configure => configure.AddConsole())
.AddDbContext<SqliteDbContext>(options => options.UseSqlite(connectionString))
;
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var mainForm = services.GetRequiredService<MainForm>();
Application.Run(mainForm);
}
catch (Exception ex)
{
Console.WriteLine("Error Occured");
}
}
}
}
}