95 lines
3.6 KiB
C#
95 lines
3.6 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OrpaonVision.ConfigApp.DependencyInjection;
|
|
using OrpaonVision.ConfigApp.Infrastructure.Diagnostics;
|
|
using OrpaonVision.ConfigApp.Infrastructure.Persistence.Options;
|
|
using OrpaonVision.ConfigApp.Infrastructure.Services;
|
|
using OrpaonVision.ConfigApp.Views;
|
|
using OrpaonVision.Core.Abstractions;
|
|
using System.Windows;
|
|
|
|
namespace OrpaonVision.ConfigApp
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
private ServiceProvider? _serviceProvider;
|
|
private GlobalExceptionHandler? _globalExceptionHandler;
|
|
|
|
/// <summary>
|
|
/// 获取应用程序的服务提供程序。
|
|
/// </summary>
|
|
public ServiceProvider ServiceProvider => _serviceProvider ?? throw new InvalidOperationException("服务提供程序未初始化。");
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(AppContext.BaseDirectory)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddConfigAppServices(configuration);
|
|
services.AddSingleton<MainWindow>();
|
|
|
|
_serviceProvider = services.BuildServiceProvider();
|
|
|
|
_globalExceptionHandler = _serviceProvider.GetRequiredService<GlobalExceptionHandler>();
|
|
_globalExceptionHandler.Register();
|
|
|
|
var logger = _serviceProvider.GetRequiredService<IAppLogger>();
|
|
logger.LogInformation("ConfigApp 启动完成。");
|
|
|
|
var persistenceOptions = _serviceProvider.GetRequiredService<PersistenceOptions>();
|
|
if (persistenceOptions.AutoInitializeOnStartup)
|
|
{
|
|
var initializer = _serviceProvider.GetRequiredService<IDataStoreInitializer>();
|
|
var initResult = initializer.Initialize();
|
|
|
|
if (!initResult.Succeeded)
|
|
{
|
|
logger.LogWarning($"数据库初始化未完成:{initResult.Code} - {initResult.Message}", initResult.TraceId);
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation("数据库初始化完成。", initResult.TraceId);
|
|
}
|
|
}
|
|
|
|
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
|
|
|
|
// 显示登录窗口
|
|
var loginWindow = new LoginWindow(
|
|
_serviceProvider.GetRequiredService<CurrentUserContext>(),
|
|
_serviceProvider.GetRequiredService<IUserService>());
|
|
|
|
var loginResult = loginWindow.ShowDialog();
|
|
if (loginResult != true)
|
|
{
|
|
// 用户取消登录,退出应用
|
|
logger.LogInformation("用户取消登录,应用退出");
|
|
Shutdown();
|
|
return;
|
|
}
|
|
|
|
mainWindow.Show();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
_serviceProvider?.GetService<IAppLogger>()?.LogInformation("ConfigApp 正在退出。");
|
|
_globalExceptionHandler?.Dispose();
|
|
_serviceProvider?.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
}
|
|
|
|
}
|