App.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Core;
  28. using TouchSocket.Sockets;
  29. using System.Net.Sockets;
  30. using TcpClient = TouchSocket.Sockets.TcpClient;
  31. using HandyControl.Controls;
  32. using Window = System.Windows.Window;
  33. namespace LogoForceTestApp
  34. {
  35. /// <summary>
  36. /// App.xaml 的交互逻辑
  37. /// </summary>
  38. public partial class App
  39. {
  40. System.Threading.Mutex mutex;
  41. public App()
  42. {
  43. this.Startup += new StartupEventHandler(App_Startup);
  44. }
  45. void App_Startup(object sender, StartupEventArgs e)
  46. {
  47. bool ret;
  48. mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);
  49. if (!ret)
  50. {
  51. //System.Windows.MessageBox.Show("已有一个程序实例运行");
  52. Environment.Exit(0);
  53. }
  54. }
  55. private static readonly string logPath = AppDomain.CurrentDomain.BaseDirectory + "logs.db";
  56. private readonly ILogger _log = new LoggerConfiguration()
  57. .WriteTo.Console(LogEventLevel.Debug)
  58. .WriteTo.Debug(LogEventLevel.Debug)
  59. .WriteTo.SQLite(logPath)
  60. .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "logs/log.log", rollingInterval: RollingInterval.Day)
  61. .CreateLogger();
  62. public static string Version { get; private set; }
  63. protected override void OnStartup(StartupEventArgs e)
  64. {
  65. var version = Assembly.GetExecutingAssembly().GetName().Version;
  66. Version = version.ToString();
  67. var teamContext = new TeamDataContext();
  68. teamContext.Database.Migrate();
  69. DispatcherUnhandledException += App_DispatcherUnhandledException;
  70. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  71. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
  72. base.OnStartup(e);
  73. }
  74. protected override Window CreateShell()
  75. {
  76. return Container.Resolve<MainWindow>();
  77. }
  78. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  79. {
  80. containerRegistry.RegisterInstance(_log);
  81. containerRegistry.RegisterSingleton<IRepository, Repository.Repository>();
  82. containerRegistry.RegisterSingleton<IModbusTcpSlaverService, ModbusTcpSlaverService>();
  83. TcpClient tcpClient = new TcpClient();
  84. //载入配置
  85. tcpClient.Setup(new TouchSocketConfig()
  86. .SetRemoteIPHost("192.168.10.200:7790")
  87. .ConfigureContainer(a =>
  88. {
  89. a.AddConsoleLogger();//添加一个日志注入
  90. }));
  91. try
  92. {
  93. tcpClient.Connect();//调用连接,当连接不成功时,会抛出异常。
  94. Growl.Info("客户端成功连接");
  95. }
  96. catch (Exception)
  97. {
  98. Growl.Warning("连接异常");
  99. }
  100. containerRegistry.RegisterInstance(tcpClient);
  101. }
  102. protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
  103. {
  104. moduleCatalog.AddModule<MainModule>();
  105. }
  106. protected override IContainerExtension CreateContainerExtension()
  107. {
  108. var services = new ServiceCollection();
  109. Serilog.Log.Logger = _log;
  110. services.AddLogging(c => c.AddSerilog());
  111. var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
  112. .AddJsonFile("appsettings.json", false, true);
  113. var configuration = builder.Build();
  114. services.ConfigureWritable<AppSettings>(configuration.GetSection(nameof(AppSettings)));
  115. services.AddHttpClient();
  116. services.AddAutoMapper(cfg =>
  117. {
  118. cfg.AddCollectionMappers();
  119. });
  120. return new DryIocContainerExtension(new DryIoc.Container(CreateContainerRules())
  121. .WithDependencyInjectionAdapter(services));
  122. }
  123. private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  124. {
  125. _log.Information($"App TaskSchedulerUnobservedTaskException Exit:{e.Exception}");
  126. }
  127. private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  128. {
  129. _log.Information($"App CurrentDomainUnhandledException Exit:{e.ExceptionObject}");
  130. }
  131. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  132. {
  133. _log.Information($"App AppDispatcherUnhandledException Exit:{e.Exception}");
  134. }
  135. }
  136. }