2025-07-19 09:24:35 +08:00
|
|
|
|
using System.Windows;
|
2025-07-26 13:35:53 +08:00
|
|
|
|
using AutoMapper.Internal;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
using DMS.Application.Services;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-07-26 13:35:53 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Core.Interfaces.Repositories;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using DMS.Helper;
|
|
|
|
|
|
using DMS.Services.Processors;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
|
|
|
|
|
using DMS.WPF.Views;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using NLog;
|
|
|
|
|
|
using DMS.Extensions;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Infrastructure.Configurations;
|
|
|
|
|
|
using DMS.Infrastructure.Data;
|
|
|
|
|
|
using DMS.Infrastructure.Repositories;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
using DMS.Infrastructure.Services;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Helper;
|
|
|
|
|
|
using DMS.WPF.Services;
|
|
|
|
|
|
using DMS.WPF.Services.Processors;
|
2025-08-22 20:46:23 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
using DataProcessingService = DMS.Services.DataProcessingService;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using IDataProcessingService = DMS.Services.IDataProcessingService;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
namespace DMS;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Interaction logic for App.xaml
|
|
|
|
|
|
/// </summary>
|
2025-07-19 09:25:01 +08:00
|
|
|
|
public partial class App : System.Windows.Application
|
2025-07-18 22:21:16 +08:00
|
|
|
|
{
|
2025-07-19 09:24:35 +08:00
|
|
|
|
public IServiceProvider Services { get; }
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// public AppSettings Settings { get; private set; }
|
2025-07-19 22:29:50 +08:00
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
public App()
|
|
|
|
|
|
{
|
|
|
|
|
|
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
|
|
|
|
|
.ConfigureServices((context, services) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureServices(services);
|
|
|
|
|
|
})
|
|
|
|
|
|
.ConfigureLogging(loggingBuilder =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureLogging(loggingBuilder);
|
|
|
|
|
|
})
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
Services = Host.Services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
|
public new static App Current => (App)System.Windows.Application.Current;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
public IHost Host { get; }
|
|
|
|
|
|
|
|
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnStartup(e);
|
2025-07-26 11:51:09 +08:00
|
|
|
|
ShutdownMode = ShutdownMode.OnLastWindowClose;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
ThemeHelper.InitializeTheme();
|
|
|
|
|
|
await Host.StartAsync();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// var databaseInitializer = Host.Services.GetRequiredService<DatabaseInitializerService>();
|
|
|
|
|
|
// databaseInitializer.InitializeDataBase();
|
|
|
|
|
|
// await databaseInitializer.InitializeMenu();
|
|
|
|
|
|
// Settings = AppSettings.Load();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
Host.Services.GetRequiredService<GrowlNotificationService>();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化数据处理链
|
|
|
|
|
|
var dataProcessingService = Host.Services.GetRequiredService<IDataProcessingService>();
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<CheckValueChangedProcessor>());
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<LoggingProcessor>());
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<MqttPublishProcessor>());
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateDbVariableProcessor>());
|
2025-07-19 09:25:01 +08:00
|
|
|
|
//dataProcessingService.AddProcessor(Host.Services.GetRequiredService<HistoryProcessor>());
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:51:09 +08:00
|
|
|
|
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
|
|
|
|
|
|
splashWindow.Show();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据配置启动服务
|
2025-07-19 22:29:50 +08:00
|
|
|
|
// var connectionSettings = DMS.Config.AppSettings.Load();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// if (connectionSettings.EnableMqttService)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Host.Services.GetRequiredService<MqttBackgroundService>().StartService();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (connectionSettings.EnableOpcUaService)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Host.Services.GetRequiredService<OpcUaBackgroundService>().StartService();
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 停止服务
|
|
|
|
|
|
await Host.StopAsync();
|
|
|
|
|
|
Host.Dispose();
|
|
|
|
|
|
LogManager.Shutdown();
|
|
|
|
|
|
base.OnExit(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// services.AddTransient<IDbContext,SqlSugarDbContext>();
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
// services.AddSingleton<IDeviceDataService, DeviceDataService>();
|
|
|
|
|
|
// services.AddSingleton<NavgatorServices>();
|
|
|
|
|
|
// //services.AddSingleton<IDialogService, DialogService>();
|
2025-07-28 13:06:36 +08:00
|
|
|
|
services.AddSingleton<GrowlNotificationService>();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// services.AddHostedService<S7BackgroundService>();
|
|
|
|
|
|
// services.AddHostedService<OpcUaBackgroundService>();
|
|
|
|
|
|
// services.AddHostedService<DMS.Infrastructure.Services.MqttBackgroundService>();
|
|
|
|
|
|
//
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
2025-07-26 13:35:53 +08:00
|
|
|
|
// --- 核心配置 ---
|
|
|
|
|
|
services.AddAutoMapper(cfg =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 最终解决方案:根据异常信息的建议,设置此标记以忽略重复的Profile加载。
|
|
|
|
|
|
// 注意:此属性位于 Internal() 方法下。
|
|
|
|
|
|
cfg.Internal().AllowAdditiveTypeMapCreation = true;
|
|
|
|
|
|
|
|
|
|
|
|
cfg.AddProfile(new DMS.Application.Profiles.MappingProfile());
|
|
|
|
|
|
cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile());
|
2025-07-27 21:08:58 +08:00
|
|
|
|
cfg.AddProfile(new DMS.WPF.Profiles.MappingProfile());
|
2025-07-26 13:35:53 +08:00
|
|
|
|
});
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 注册数据处理服务和处理器
|
|
|
|
|
|
services.AddSingleton<IDataProcessingService, DataProcessingService>();
|
|
|
|
|
|
services.AddHostedService(provider => (DataProcessingService)provider.GetRequiredService<IDataProcessingService>());
|
|
|
|
|
|
services.AddSingleton<CheckValueChangedProcessor>();
|
|
|
|
|
|
services.AddSingleton<LoggingProcessor>();
|
|
|
|
|
|
services.AddSingleton<UpdateDbVariableProcessor>();
|
|
|
|
|
|
services.AddSingleton<HistoryProcessor>();
|
|
|
|
|
|
services.AddSingleton<MqttPublishProcessor>();
|
|
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
// 注册Core中的仓库
|
|
|
|
|
|
services.AddSingleton<AppSettings>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
// services.AddSingleton<SqlSugarDbContext>();
|
|
|
|
|
|
// 2. 配置数据库上下文 (在测试中通常使用单例)
|
|
|
|
|
|
services.AddSingleton<SqlSugarDbContext>(_ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var appSettings = new AppSettings { Database = { Database = "dms_test" } };
|
|
|
|
|
|
return new SqlSugarDbContext(appSettings);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<IInitializeRepository, InitializeRepository>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddSingleton<IRepositoryManager, RepositoryManager>();
|
2025-08-22 20:24:09 +08:00
|
|
|
|
services.AddSingleton<IExcelService, ExcelService>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
// 注册App服务
|
|
|
|
|
|
services.AddSingleton<IInitializeService,InitializeService>();
|
|
|
|
|
|
services.AddSingleton<IDeviceAppService,DeviceAppService>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddSingleton<IVariableAppService,VariableAppService>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableAppService,VariableTableAppService>();
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<INavigationService, NavigationService>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddSingleton<IMenuService, MenuService>();
|
2025-07-26 18:58:52 +08:00
|
|
|
|
services.AddSingleton<IDialogService, DialogService>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
//注册WPF中的服务
|
|
|
|
|
|
services.AddSingleton<DataServices>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// 注册视图模型
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<SplashViewModel>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<MainViewModel>();
|
|
|
|
|
|
services.AddSingleton<HomeViewModel>();
|
|
|
|
|
|
services.AddSingleton<DevicesViewModel>();
|
|
|
|
|
|
services.AddSingleton<DataTransformViewModel>();
|
|
|
|
|
|
services.AddSingleton<SettingViewModel>();
|
|
|
|
|
|
services.AddSingleton<DataTransformViewModel>();
|
|
|
|
|
|
services.AddSingleton<VariableTableViewModel>();
|
2025-07-19 14:36:34 +08:00
|
|
|
|
//services.AddScoped<MqttServerDetailViewModel>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<DeviceDetailViewModel>();
|
2025-07-26 18:58:52 +08:00
|
|
|
|
services.AddSingleton<MqttsViewModel>();
|
2025-08-22 20:46:23 +08:00
|
|
|
|
// 注册对话框模型
|
|
|
|
|
|
services.AddTransient<ImportExcelDialogViewModel>();
|
2025-07-27 21:08:58 +08:00
|
|
|
|
// 注册对话框
|
|
|
|
|
|
services.AddSingleton<DevicesView>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
//注册View视图
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<SplashWindow>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<SettingView>();
|
|
|
|
|
|
services.AddSingleton<MainView>();
|
|
|
|
|
|
services.AddSingleton<HomeView>();
|
|
|
|
|
|
services.AddSingleton<DevicesView>();
|
|
|
|
|
|
services.AddSingleton<VariableTableView>();
|
|
|
|
|
|
services.AddScoped<DeviceDetailView>();
|
|
|
|
|
|
services.AddScoped<MqttsView>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogManager.Setup().LoadConfigurationFromFile("Config/nlog.config");
|
|
|
|
|
|
loggingBuilder.ClearProviders();
|
|
|
|
|
|
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// loggingBuilder.AddNLog();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 捕获未处理的异常并记录
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var ex = args.ExceptionObject as Exception;
|
|
|
|
|
|
if (ex != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 可以使用一个专用的 Logger 来记录未处理异常
|
|
|
|
|
|
LogManager.GetLogger("UnhandledExceptionLogger")
|
|
|
|
|
|
.Fatal($"应用程序发生未处理的异常:{ex}");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 捕获 Dispatcher 线程上的未处理异常 (UI 线程)
|
|
|
|
|
|
this.DispatcherUnhandledException += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LogManager.GetLogger("DispatcherExceptionLogger")
|
|
|
|
|
|
.Fatal($"UI 线程发生未处理的异常:{args.Exception}");
|
|
|
|
|
|
// 标记为已处理,防止应用程序崩溃 (生产环境慎用,可能掩盖问题)
|
|
|
|
|
|
// args.Handled = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 如果您使用 Task (异步方法) 并且没有正确 await,可能会导致异常丢失,
|
|
|
|
|
|
// 可以通过以下方式捕获 Task 中的异常。
|
|
|
|
|
|
TaskScheduler.UnobservedTaskException += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LogManager.GetLogger("UnobservedTaskExceptionLogger")
|
|
|
|
|
|
.Fatal($"异步任务发生未观察到的异常:{args.Exception}");
|
|
|
|
|
|
// args.SetObserved(); // 标记为已观察,防止进程终止
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 09:25:01 +08:00
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|