Skip to content

Commit

Permalink
[feat]新增Runtime.CreateConfigOnMissing 默认配置。配置文件不存在时,是否生成默认配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 16, 2024
1 parent 24c7919 commit 5a55467
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 19 additions & 0 deletions NewLife.Core/Common/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,23 @@ public static Int64 TickCount64
return dic;
}
#endregion

#region 设置
private static Boolean? _createConfigOnMissing;
/// <summary>默认配置。配置文件不存在时,是否生成默认配置文件</summary>
public static Boolean CreateConfigOnMissing
{
get
{
if (_createConfigOnMissing == null)
{
var val = Environment.GetEnvironmentVariable("CreateConfigOnMissing");
_createConfigOnMissing = !val.IsNullOrEmpty() ? val.ToBoolean(true) : true;
}

return _createConfigOnMissing.Value;
}
set { _createConfigOnMissing = value; }
}
#endregion
}
15 changes: 10 additions & 5 deletions NewLife.Core/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static TConfig Current
try
{
// OnLoad 中可能有变化,存回去
prv.Save(config);
//prv.Save(config);
config.Save();
}
catch { }

Expand All @@ -94,10 +95,14 @@ protected virtual void OnLoaded() { }

/// <summary>保存到配置文件中去</summary>
//[Obsolete("=>Provider.Save")]
public virtual void Save() => Provider?.Save(this);
public virtual void Save()
{
var prv = Provider;
if (prv == null) return;

///// <summary>异步保存</summary>
//[Obsolete("=>Provider.Save")]
//public virtual void SaveAsync() => ThreadPoolX.QueueUserWorkItem(() => Provider.Save(this));
// 是否创建默认配置
if (!prv.IsNew || Runtime.CreateConfigOnMissing)
prv.Save(this);
}
#endregion
}

0 comments on commit 5a55467

Please sign in to comment.