Files
Explorer-Workbench/src/Explorer.App/App.xaml.cs

101 lines
3.0 KiB
C#

using System.IO;
using System.Windows;
using Explorer.Hosting;
using Explorer.Hosting.Ipc;
using Explorer.Presentation.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace Explorer.App;
public partial class App : System.Windows.Application
{
private IHost? _host;
private WorkbenchPipeClient? _workbenchClient;
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
DispatcherUnhandledException += (_, args) =>
{
Log.Error(args.Exception, "Unhandled UI exception");
args.Handled = true;
};
var env = new Windows.WindowsAppEnvironment();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File(
Path.Combine(env.LogDirectory, "explorer-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 14)
.CreateLogger();
_workbenchClient = await WorkbenchHostConnector.ConnectOrStartAsync(
TimeSpan.FromSeconds(12),
logger: null).ConfigureAwait(true);
var hostExe = HostLogonAutostart.FindHostExecutable();
if (_workbenchClient is null && hostExe is not null)
{
MessageBox.Show(
"Explorer.Host.exe is present but the window could not connect to it. See logs.",
"Explorer Workbench",
MessageBoxButton.OK,
MessageBoxImage.Error);
Shutdown(-1);
return;
}
_host = Host.CreateDefaultBuilder()
.UseSerilog()
.ConfigureServices((_, services) =>
{
if (_workbenchClient is not null)
{
services.AddExplorerClient(_workbenchClient);
services.AddExplorerUi();
}
else
{
services.AddExplorer();
}
})
.Build();
var vm = _host.Services.GetRequiredService<MainViewModel>();
var window = _host.Services.GetRequiredService<MainWindow>();
vm.PrepareUi();
window.DataContext = vm;
window.Show();
try
{
await vm.InitializeAsync().ConfigureAwait(true);
}
catch (Exception ex)
{
Log.Error(ex, "Startup initialization failed");
vm.Footer = "Started with errors. See logs.";
}
await _host.StartAsync().ConfigureAwait(true);
}
protected override async void OnExit(ExitEventArgs e)
{
if (_host is not null)
{
await _host.StopAsync(TimeSpan.FromSeconds(3)).ConfigureAwait(true);
_host.Dispose();
}
if (_workbenchClient is not null)
{
await _workbenchClient.DisposeAsync().ConfigureAwait(true);
}
Log.CloseAndFlush();
base.OnExit(e);
}
}