App.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using LogoForceTestApp.Modules.MainModule.Models;
  2. using LogoForceTestApp.Modules.MainModule;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Configuration;
  5. using Prism.DryIoc;
  6. using Prism.Ioc;
  7. using Prism.Modularity;
  8. using Serilog.Events;
  9. using Serilog;
  10. using System;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Threading;
  15. using LogoForceTestApp.Services.Interfaces;
  16. using LogoForceTestApp.Services;
  17. using LogoForceTestApp.Views;
  18. using DryIoc;
  19. using Team.Utility;
  20. using DryIoc.Microsoft.DependencyInjection;
  21. using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
  22. using Repository;
  23. using Microsoft.EntityFrameworkCore;
  24. using AutoMapper.EquivalencyExpression;
  25. using System.Linq;
  26. using System.Reflection;
  27. using TouchSocket.Sockets;
  28. using System.Text;
  29. using TouchSocket.Core;
  30. using LogoForceTestApp.Modules.MainModule.Views;
  31. using Prism.Events;
  32. using AutoMapper;
  33. using Mapper = AutoMapper.Mapper;
  34. using LogForceTestApp.Modules.MainModule;
  35. namespace LogoForceTestApp
  36. {
  37. /// <summary>
  38. /// App.xaml 的交互逻辑
  39. /// </summary>
  40. public partial class App
  41. {
  42. private static readonly string logPath = AppDomain.CurrentDomain.BaseDirectory + "logs.db";
  43. private readonly ILogger _log = new LoggerConfiguration()
  44. .WriteTo.Console(LogEventLevel.Debug)
  45. .WriteTo.Debug(LogEventLevel.Debug)
  46. .WriteTo.SQLite(logPath)
  47. .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "logs/log.log", rollingInterval: RollingInterval.Day)
  48. .CreateLogger();
  49. public static string Version { get; private set; }
  50. protected override void OnStartup(StartupEventArgs e)
  51. {
  52. var version =Assembly.GetExecutingAssembly().GetName().Version;
  53. Version = version.ToString();
  54. var teamContext = new TeamDataContext();
  55. teamContext.Database.Migrate();
  56. DispatcherUnhandledException += App_DispatcherUnhandledException;
  57. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  58. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  59. base.OnStartup(e);
  60. }
  61. protected override Window CreateShell()
  62. {
  63. Container.Resolve<KBWindow>().Show();
  64. //new KBWindow().Show();
  65. return Container.Resolve<MainWindow>();
  66. }
  67. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  68. {
  69. containerRegistry.RegisterSingleton<KBWindow>();
  70. containerRegistry.RegisterSingleton<MulProductModel>();
  71. containerRegistry.RegisterInstance(_log);
  72. containerRegistry.RegisterSingleton<IRepository, Repository.Repository>();
  73. containerRegistry.RegisterSingleton<IModbusTcpSlaverService, ModbusTcpSlaverService>();
  74. var configuration = new MapperConfiguration(cfg =>
  75. {
  76. cfg.AddCollectionMappers();
  77. cfg.AddProfile(new MainModuleProfile());
  78. });
  79. containerRegistry.RegisterInstance(typeof(IMapper), new Mapper(configuration));
  80. //tcp
  81. TcpService tcpService = new TcpService();
  82. tcpService.Setup(new TouchSocketConfig()//载入配置
  83. .SetListenIPHosts(7790)//端口号
  84. .ConfigureContainer(a =>//容器的配置顺序应该在最前面
  85. {
  86. a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
  87. }));
  88. tcpService.Start();//启动
  89. containerRegistry.RegisterInstance(tcpService);
  90. }
  91. protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
  92. {
  93. moduleCatalog.AddModule<MainModule>();
  94. }
  95. protected override IContainerExtension CreateContainerExtension()
  96. {
  97. var services = new ServiceCollection();
  98. Serilog.Log.Logger = _log;
  99. services.AddLogging(c => c.AddSerilog());
  100. var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
  101. .AddJsonFile("appsettings.json", false, true);
  102. var configuration = builder.Build();
  103. services.ConfigureWritable<AppSettings>(configuration.GetSection(nameof(AppSettings)));
  104. services.AddHttpClient();
  105. return new DryIocContainerExtension(new DryIoc.Container(CreateContainerRules())
  106. .WithDependencyInjectionAdapter(services));
  107. }
  108. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  109. {
  110. _log.Information($"App TaskSchedulerUnobservedTaskException Exit:{e.Exception}");
  111. }
  112. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  113. {
  114. _log.Information($"App CurrentDomainUnhandledException Exit:{e.ExceptionObject}");
  115. }
  116. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  117. {
  118. _log.Information($"App AppDispatcherUnhandledException Exit:{e.Exception}");
  119. }
  120. }
  121. }