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-09-04 13:40:07 +08:00
|
|
|
|
using DMS.Application.Services.Processors;
|
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-09-04 14:46:50 +08:00
|
|
|
|
using DMS.Core.Interfaces.Services;
|
2025-09-04 21:38:56 +08:00
|
|
|
|
using DMS.Infrastructure.Configuration;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Infrastructure.Configurations;
|
|
|
|
|
|
using DMS.Infrastructure.Data;
|
2025-09-05 15:59:14 +08:00
|
|
|
|
using DMS.Infrastructure.Interfaces;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.Infrastructure.Interfaces.Services;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Infrastructure.Repositories;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
using DMS.Infrastructure.Services;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Helper;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
|
|
|
|
|
using DMS.WPF.Logging;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
2025-08-22 20:46:23 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.Views;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using NLog;
|
2025-09-05 16:18:01 +08:00
|
|
|
|
using NLog.Web;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
|
2025-09-04 14:46:50 +08:00
|
|
|
|
namespace DMS.WPF;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-04 21:04:27 +08:00
|
|
|
|
/// Interaction logic for App.xaml
|
2025-07-19 09:24:35 +08:00
|
|
|
|
/// </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-09-04 21:04:27 +08:00
|
|
|
|
public new static App Current => (App)System.Windows.Application.Current;
|
|
|
|
|
|
public IHost Host { get; }
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
public App()
|
|
|
|
|
|
{
|
|
|
|
|
|
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
2025-09-04 21:04:27 +08:00
|
|
|
|
.ConfigureServices((context, services) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureServices(services);
|
|
|
|
|
|
})
|
|
|
|
|
|
.ConfigureLogging(loggingBuilder =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureLogging(loggingBuilder);
|
|
|
|
|
|
})
|
|
|
|
|
|
.Build();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
Services = Host.Services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-09-04 19:59:35 +08:00
|
|
|
|
var notificationService = Host.Services.GetRequiredService<INotificationService>();
|
2025-09-04 17:29:24 +08:00
|
|
|
|
notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 11:51:09 +08:00
|
|
|
|
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
|
|
|
|
|
|
splashWindow.Show();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 停止服务
|
|
|
|
|
|
await Host.StopAsync();
|
|
|
|
|
|
Host.Dispose();
|
|
|
|
|
|
LogManager.Shutdown();
|
|
|
|
|
|
base.OnExit(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2025-09-04 14:46:50 +08:00
|
|
|
|
// 注册NLogLogger作为Microsoft.Extensions.Logging.ILogger的实现
|
2025-09-05 16:18:01 +08:00
|
|
|
|
services.AddSingleton<ILogger, NLogLogger>();
|
2025-09-04 14:46:50 +08:00
|
|
|
|
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
|
2025-07-28 13:06:36 +08:00
|
|
|
|
services.AddSingleton<GrowlNotificationService>();
|
2025-09-04 19:59:35 +08:00
|
|
|
|
services.AddSingleton<INotificationService, NotificationService>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
2025-09-04 21:04:27 +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
|
|
|
|
|
|
|
|
|
|
// 注册数据处理服务和处理器
|
2025-09-04 21:38:56 +08:00
|
|
|
|
// services.AddHostedService<OpcUaBackgroundService>();
|
2025-09-05 15:59:14 +08:00
|
|
|
|
//注册OpcUa相关的服务
|
2025-09-04 21:38:56 +08:00
|
|
|
|
services.Configure<OpcUaServiceOptions>(options => { });
|
2025-09-04 21:04:27 +08:00
|
|
|
|
services.AddSingleton<IOpcUaServiceManager, OpcUaServiceManager>();
|
2025-09-04 21:38:56 +08:00
|
|
|
|
services.AddHostedService<OptimizedOpcUaBackgroundService>();
|
2025-09-05 15:59:14 +08:00
|
|
|
|
// 注册S7相关的服务
|
|
|
|
|
|
services.AddSingleton<IS7ServiceFactory, S7ServiceFactory>();
|
|
|
|
|
|
services.AddSingleton<IS7ServiceManager, S7ServiceManager>();
|
|
|
|
|
|
services.AddSingleton<IChannelBus, ChannelBus>();
|
|
|
|
|
|
services.AddSingleton<IMessenger, Messenger>();
|
|
|
|
|
|
services.AddHostedService<OptimizedS7BackgroundService>();
|
|
|
|
|
|
|
2025-09-04 21:38:56 +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>(_ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
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-09-04 17:29:24 +08:00
|
|
|
|
services.AddSingleton<IDeviceRepository, DeviceRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableRepository, VariableTableRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableRepository, VariableRepository>();
|
|
|
|
|
|
services.AddSingleton<IMqttServerRepository, MqttServerRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableMqttAliasRepository, VariableMqttAliasRepository>();
|
|
|
|
|
|
services.AddSingleton<IMenuRepository, MenuRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableHistoryRepository, VariableHistoryRepository>();
|
|
|
|
|
|
services.AddSingleton<IUserRepository, UserRepository>();
|
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-08-25 20:16:57 +08:00
|
|
|
|
|
2025-09-01 21:03:34 +08:00
|
|
|
|
services.AddTransient<IOpcUaService, OpcUaService>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
// 注册App服务
|
2025-09-04 21:04:27 +08:00
|
|
|
|
services.AddSingleton<IInitializeService, InitializeService>();
|
|
|
|
|
|
services.AddSingleton<IDeviceAppService, DeviceAppService>();
|
|
|
|
|
|
services.AddSingleton<IVariableAppService, VariableAppService>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableAppService, VariableTableAppService>();
|
2025-09-03 11:12:42 +08:00
|
|
|
|
services.AddSingleton<IMenuService, MenuService>();
|
2025-09-02 20:28:29 +08:00
|
|
|
|
services.AddSingleton<IDataCenterService, DataCenterService>();
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<INavigationService, NavigationService>();
|
2025-07-26 18:58:52 +08:00
|
|
|
|
services.AddSingleton<IDialogService, DialogService>();
|
2025-09-04 21:04:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 注册WPF中的服务
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddSingleton<DataServices>();
|
2025-09-04 21:04:27 +08:00
|
|
|
|
|
2025-09-04 21:38:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>();
|
2025-08-24 18:29:26 +08:00
|
|
|
|
services.AddTransient<VariableTableViewModel>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<DeviceDetailViewModel>();
|
2025-07-26 18:58:52 +08:00
|
|
|
|
services.AddSingleton<MqttsViewModel>();
|
2025-09-04 21:04:27 +08:00
|
|
|
|
|
2025-08-22 20:46:23 +08:00
|
|
|
|
// 注册对话框模型
|
|
|
|
|
|
services.AddTransient<ImportExcelDialogViewModel>();
|
2025-08-25 20:16:57 +08:00
|
|
|
|
services.AddTransient<ImportOpcUaDialogViewModel>();
|
2025-08-24 14:42:31 +08:00
|
|
|
|
services.AddTransient<VariableDialogViewModel>();
|
2025-09-04 21:04:27 +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)
|
|
|
|
|
|
{
|
2025-09-04 14:46:50 +08:00
|
|
|
|
LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
loggingBuilder.ClearProviders();
|
|
|
|
|
|
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
2025-09-05 16:18:01 +08:00
|
|
|
|
// loggingBuilder.addn; // 添加NLog作为日志提供者
|
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")
|
2025-09-04 21:04:27 +08:00
|
|
|
|
.Fatal($"应用程序发生未处理的异常:{ex}");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 捕获 Dispatcher 线程上的未处理异常 (UI 线程)
|
|
|
|
|
|
this.DispatcherUnhandledException += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LogManager.GetLogger("DispatcherExceptionLogger")
|
2025-09-04 21:04:27 +08:00
|
|
|
|
.Fatal($"UI 线程发生未处理的异常:{args.Exception}");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// 标记为已处理,防止应用程序崩溃 (生产环境慎用,可能掩盖问题)
|
|
|
|
|
|
// args.Handled = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 如果您使用 Task (异步方法) 并且没有正确 await,可能会导致异常丢失,
|
|
|
|
|
|
// 可以通过以下方式捕获 Task 中的异常。
|
|
|
|
|
|
TaskScheduler.UnobservedTaskException += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
LogManager.GetLogger("UnobservedTaskExceptionLogger")
|
2025-09-04 21:04:27 +08:00
|
|
|
|
.Fatal($"异步任务发生未观察到的异常:{args.Exception}");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// args.SetObserved(); // 标记为已观察,防止进程终止
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|