|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
|
|
|
|
|
|
namespace DS.Module.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置读取类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AppSetting
|
|
|
|
|
{
|
|
|
|
|
public static IConfiguration Configuration { get; set; }
|
|
|
|
|
|
|
|
|
|
static AppSetting()
|
|
|
|
|
{
|
|
|
|
|
string Path = "appsettings.json";
|
|
|
|
|
//ReloadOnChange = true 当appsettings.json被修改时重新加载
|
|
|
|
|
Configuration = new ConfigurationBuilder()
|
|
|
|
|
// .SetBasePath(contentPath)
|
|
|
|
|
.Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })
|
|
|
|
|
.Add(new JsonConfigurationSource { Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", Optional = false, ReloadOnChange = true })
|
|
|
|
|
.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
public AppSetting(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 封装要操作的字符
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sections">节点配置</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string app(params string[] sections)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (sections.Any())
|
|
|
|
|
{
|
|
|
|
|
return Configuration[string.Join(":", sections)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception) { }
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 递归获取配置信息数组
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="sections"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<T> app<T>(params string[] sections)
|
|
|
|
|
{
|
|
|
|
|
List<T> list = new List<T>();
|
|
|
|
|
// 引用 Microsoft.Extensions.Configuration.Binder 包
|
|
|
|
|
Configuration.Bind(string.Join(":", sections), list);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据路径 configuration["App:Name"];
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sectionsPath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetValue(string sectionsPath)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Configuration[sectionsPath];
|
|
|
|
|
}
|
|
|
|
|
catch (Exception) { }
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|