添加项目文件。
This commit is contained in:
40
App.xaml.cs
Normal file
40
App.xaml.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using MembranePoreTester.Communication;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MembranePoreTester
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
public static IPlcService PlcService { get; private set; }
|
||||
public static PlcConfiguration PlcConfig { get; private set; }
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
|
||||
using var db = new AppDbContext();
|
||||
db.Database.EnsureCreated(); // 自动建表
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||||
|
||||
var configuration = builder.Build();
|
||||
PlcConfig = configuration.GetSection("PlcSettings").Get<PlcConfiguration>();
|
||||
|
||||
if (PlcConfig == null)
|
||||
throw new InvalidOperationException("PLC settings missing in appsettings.json");
|
||||
|
||||
PlcService = new ModbusTcpPlcService(PlcConfig);
|
||||
}
|
||||
|
||||
protected override void OnExit(ExitEventArgs e)
|
||||
{
|
||||
(PlcService as IDisposable)?.Dispose();
|
||||
base.OnExit(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user