You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.4 KiB
C#
112 lines
4.4 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using Avalonia;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
using Avalonia.Markup.Xaml;
|
|
using Com.Ctrip.Framework.Apollo;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NeoSmart.Caching.Sqlite;
|
|
using Sln.Wcs.Repository;
|
|
using Sln.Wcs.Serilog;
|
|
using Sln.Wcs.UI.ViewModels;
|
|
using Sln.Wcs.UI.Views;
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
|
|
|
|
namespace Sln.Wcs.UI;
|
|
|
|
public partial class App : Application
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
AvaloniaXamlLoader.Load(this);
|
|
}
|
|
|
|
public override void OnFrameworkInitializationCompleted()
|
|
{
|
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
|
{
|
|
// ---- 独立初始化 WCS 后端服务 ----
|
|
var services = new ServiceCollection();
|
|
|
|
// 1. Apollo 配置中心
|
|
var basePath = AppContext.BaseDirectory;
|
|
var localConfig = new ConfigurationBuilder()
|
|
.SetBasePath(basePath)
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.Build();
|
|
|
|
services.AddSingleton<IConfiguration>(localConfig);
|
|
|
|
var apolloConfigSection = localConfig.GetSection("apollo");
|
|
var apolloConfig = new ConfigurationBuilder()
|
|
.SetBasePath(basePath)
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddApollo(apolloConfigSection)
|
|
.AddDefault()
|
|
.Build();
|
|
|
|
// 替换为 Apollo 增强配置
|
|
services.Remove(new ServiceDescriptor(typeof(IConfiguration), localConfig));
|
|
services.AddSingleton<IConfiguration>(apolloConfig);
|
|
|
|
// 2. 加载程序集并扫描注册 DI
|
|
var assemblies = new[]
|
|
{
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Common.dll")),
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Cache.dll")),
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Repository.dll")),
|
|
};
|
|
|
|
services.Scan(scan => scan.FromAssemblies(assemblies)
|
|
.AddClasses()
|
|
.AsImplementedInterfaces()
|
|
.AsSelf()
|
|
.WithTransientLifetime());
|
|
|
|
// 3. 注册日志
|
|
services.AddSingleton(typeof(SerilogHelper));
|
|
|
|
// 4. SqlSugar + FusionCache
|
|
services.AddSqlSugarSetup();
|
|
services.AddFusionCache()
|
|
.WithSerializer(new FusionCacheNewtonsoftJsonSerializer())
|
|
.WithDistributedCache(new SqliteCache(new SqliteCacheOptions
|
|
{
|
|
CachePath = apolloConfig["cachePath"]!
|
|
}));
|
|
|
|
// 5. 注册 ViewModel
|
|
services.AddSingleton<ViewModels.NavigationViewModel>();
|
|
services.AddSingleton<ViewModels.HomePageViewModel>();
|
|
services.AddSingleton<ViewModels.SystemMonitorViewModel>();
|
|
services.AddTransient<ViewModels.Device.DeviceHostViewModel>();
|
|
services.AddTransient<ViewModels.Device.DeviceInfoViewModel>();
|
|
services.AddTransient<ViewModels.Device.DeviceParamViewModel>();
|
|
services.AddTransient<ViewModels.Base.LocationInfoViewModel>();
|
|
services.AddTransient<ViewModels.Base.MaterialInfoViewModel>();
|
|
services.AddTransient<ViewModels.Base.StoreInfoViewModel>();
|
|
services.AddTransient<ViewModels.Path.PathInfoViewModel>();
|
|
services.AddTransient<ViewModels.Path.PathDetailsViewModel>();
|
|
services.AddTransient<ViewModels.Task.TaskQueueViewModel>();
|
|
services.AddTransient<ViewModels.Task.TaskDetailViewModel>();
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
// ---- 启动后初始化 ----
|
|
|
|
// 启用 Serilog
|
|
serviceProvider.UseSerilogExtensions();
|
|
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
|
var log = serviceProvider.GetRequiredService<SerilogHelper>();
|
|
log.Info($"系统启动成功,日志存放位置:{config["logPath"]}");
|
|
|
|
// 创建主窗口
|
|
desktop.MainWindow = new MainWindow(serviceProvider.GetRequiredService<ViewModels.NavigationViewModel>());
|
|
}
|
|
|
|
base.OnFrameworkInitializationCompleted();
|
|
}
|
|
}
|