using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.Domain.Services.BuildingsiteServices;
using DaSaSo.Domain.Services.ProjectServices;
using DaSaSo.Domain.Services.SewerObjectService;
using DaSaSo.EntityFramework;
using DaSaSo.EntityFramework.Services;
using DaSaSo.Wpf.ViewModel;
using DaSaSo.Wpf.ViewModel.Factories;
using DaSaSo.Wpf.ViewModel.Interface;
using DaSaSo.Wpf.ViewModel.State.ActualState;
using DaSaSo.Wpf.ViewModel.State.Navigation;
using DaSaSo.Wpf.HostBuilders;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Windows;
namespace DaSaSo.Wpf
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
private readonly IHost _host;
public App()
{
_host = CreateHostBuilder().Build();
}
public static IHostBuilder CreateHostBuilder(string[]? args = null)
{
return Host.CreateDefaultBuilder(args)
.AddConfiguration()
.AddServices()
.AddViewModels()
.AddStores()
.AddDBContext();
}
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
_host.Start();
DaSaSoDbContextFactory contextFactory = _host.Services.GetRequiredService();
using (DaSaSoDbContext context = contextFactory.CreateDbContext())
{
context.Database.Migrate();
}
MainWindow? window = new MainWindow() { DataContext = _host.Services.GetRequiredService() };
window.Show();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
await _host.StopAsync();
_host.Dispose();
base.OnExit(e);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string text = "An application error occured. Please contact the Administrator with the following information:\n\n";
MessageBox.Show(text + " " + ex.Message + "\n\n" + ex.StackTrace);
}
catch(Exception ex2)
{
MessageBox.Show("Fatal Non-UI error", ex2.Message, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
throw new NotImplementedException();
}
}
}