在基于MSI的安装过程中,我们将通过参数和/或安装程序表格获取设置值。
我正在尝试动态更新应用程序appsettings文件的json属性,但不确定如何在任何深度使用“ node:child:child”键格式来搜索json。 所有选项都在表上,我们能够序列化该对象并使用反射,linq解决方案或其他任何方法。这与JSON替换之类的东西在Azure构建或发布管道中的工作原理非常相似。
除了我无法从IConfiguration中提取json字符串以将其写回到文件中之外,目前非常接近。
public static ActionResult ModifySettings(Session session)
{
session.Log("Begin ModifySettings");
try
{
string[] customData = session.CustomActionData.Keys.ToArray();
string[] customValues = session.CustomActionData.Values.ToArray();
///Product.wxs -> BinDir=[BinDir]
var installDirectory = customValues[1];
string file = Path.Combine(installDirectory, "appsettings.json");
session.Log(string.Format($"Updating file: {file}.{Environment.NewLine}."));
if (File.Exists(file))
{
session.Log($"Updating Settings file: {file}.{Environment.NewLine}");
var configFile = new ConfigurationBuilder().AddJsonFile(file).Build();
for (int i = 3; i < customData.Count(); i++)
{
if (!ReplaceSetting(configFile, customData[i], customValues[i], session))
throw new ArgumentException($"Error during settings replacement of {customData[i]} with {customValues[i]}",customData[i]);
}
var settingsAsString = configFile.ToString();//DOES NOT WORK
session.Log($"Updated settings{Environment.NewLine}{settingsAsString}{Environment.NewLine}");
File.WriteAllText(file, settingsAsString);
}
else
{
session.Log($"Failed to update file {file}.{Environment.NewLine}Exiting.");
return ActionResult.Failure;
}
return ActionResult.Success;
}catch(Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
private static bool ReplaceSetting(IConfigurationRoot settings, string settingName, string settingValue, Session session)
{
try
{
session.Log(string.Format($"Updating Setting: {settingName}{Environment.NewLine}Value: {settingValue}{Environment.NewLine}"));
settings[settingName] = settingValue; //<- THIS IS THE PART I NEED TO MIMIC WHEN SETTINGNAME FORMAT IS SOMETHING:SOMETHINGELSE:PROPERTY!!!
}
catch(Exception ex)
{
session.Log(ex.Message);
return false;
}
return true;
}
使用JObject的EDIT工作解决方案
public static ActionResult ModifySettings(Session session)
{
session.Log("Begin ModifySettings");
try
{
string[] customData = session.CustomActionData.Keys.ToArray();
string[] customValues = session.CustomActionData.Values.ToArray();
///Product.wxs -> BinDir=[BinDir]
var installDirectory = customValues[1];
string file = Path.Combine(installDirectory, "appsettings.json");
session.Log(string.Format($"Updating file: {file}.{Environment.NewLine}."));
if (File.Exists(file))
{
session.Log($"Updating Settings file: {file}.{Environment.NewLine}");
var configFile = new ConfigurationBuilder().AddJsonFile(file).Build();
JObject settings = JObject.Parse(File.ReadAllText(file));
for (int i = 3; i < customData.Count(); i++)
{
if (!ReplaceSetting(ref settings, customData[i], customValues[i], session))
throw new ArgumentException($"Error during settings replacement of {customData[i]} with {customValues[i]}",customData[i]);
}
session.Log($"Updated settings{Environment.NewLine}{settings.ToString()}{Environment.NewLine}");
File.WriteAllText(file, settings.ToString());
}
else
{
session.Log($"Failed to update file {file}.{Environment.NewLine}Exiting.");
return ActionResult.Failure;
}
return ActionResult.Success;
}catch(Exception ex)
{
session.Log(ex.Message);
return ActionResult.Failure;
}
}
private static bool ReplaceSetting(ref JObject settingFile, string settingName, string settingValue, Session session)
{
try
{
session.Log(string.Format($"Updating Setting: {settingName}{Environment.NewLine}Value: {settingValue}{Environment.NewLine}"));
var token = settingFile.SelectToken(settingName);
(token.Parent as JProperty).Value = settingValue;
}
catch (Exception ex)
{
session.Log(ex.Message);
return false;
}
return true;
}