generated from wenjy/Sln.Iot
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.
34 lines
1012 B
C#
34 lines
1012 B
C#
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
namespace Sln.Iot.Config
|
|
{
|
|
public class AppConfigSetting
|
|
{
|
|
private static AppConfig? _config;
|
|
|
|
/// <summary>
|
|
/// 加载配置文件并解析为 AppConfig 对象。
|
|
/// </summary>
|
|
public static AppConfig Load()
|
|
{
|
|
if (_config != null)
|
|
return _config; // 已经加载过就直接返回(模拟单例)
|
|
|
|
var configBuilder = new ConfigurationBuilder()
|
|
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
|
|
|
IConfiguration configuration = configBuilder.Build();
|
|
_config = configuration.GetSection("AppConfig").Get<AppConfig>();
|
|
|
|
if (_config == null)
|
|
{
|
|
throw new InvalidOperationException("无法从 appsettings.json 加载 AppConfig 配置。");
|
|
}
|
|
|
|
return _config;
|
|
}
|
|
}
|
|
}
|