我正在使用WiX v3.14来构建.Net Core安装程序。我有一个用C#编写的CustomAction-UpdateJsonAppSettings-,它旨在更新appsettings.json文件,该文件是安装的一部分(使用从执行安装的用户输入的字段构建的数据库连接字符串)。 当我将CustomAction安排为立即运行,以After =“ InstallFinalize”(已通过Before =“ UpdateJsonAppSettings”安排的CustomActionData集合)运行时,session.CustomActionData集合为空。
<CustomAction Id="SetCustomActionData"
Return="check"
Property="UpdateJsonAppSettings"
Value="connectionString=[CONNECTION_STRING_FORMATTED];filepath=[PATH_TO_APPSETTINGS_JSON]" />
<CustomAction Id="UpdateJsonAppSettings"
BinaryKey="CustomActions"
DllEntry="UpdateJsonAppSettings"
Execute="immediate"
Return="ignore" />
<InstallExecuteSequence>
<Custom Action="ConnectionString" Before="SetCustomActionData" />
<Custom Action="SetCustomActionData" Before="UpdateJsonAppSettings" />
<Custom Action="UpdateJsonAppSettings" After="InstallFinalize">NOT Installed AND NOT PATCH</Custom>
</InstallExecuteSequence>
Session.Log:
Session.Log:
MSI (s) (C8:8C) [11:15:44:363]: Doing action: SetCustomActionData
Action 11:15:44: SetCustomActionData.
Action start 11:15:44: SetCustomActionData.
MSI (s) (C8:8C) [11:15:44:379]: PROPERTY CHANGE: Adding UpdateJsonAppSettings property. Its value is 'connectionString=Data Source=localhost\SQLEXPRESS;;Initial Catalog=DB;;User Id=dbuser;;Password=dbuserpassword;;MultipleActiveResultSets=true;;App=EntityFramework;filepath=[#appSettings]'.
Action ended 11:15:44: SetCustomActionData. Return value 1.
[SNIP]
Action start 11:15:44: UpdateJsonAppSettings.
MSI (s) (C8:B8) [11:15:44:382]: Invoking remote custom action. DLL: C:\windows\Installer\MSI95BF.tmp, Entrypoint: UpdateJsonAppSettings
SFXCA: Extracting custom action to temporary directory: C:\TEMP\MSI95BF.tmp-\
SFXCA: Binding to CLR version v4.0.30319
Calling custom action CustomActions!CustomActions.CustomAction.UpdateJsonAppSettings
Session.CustomActionData.Count(): 0
Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
当我将CustomAction UpdateJsonAppSettings修改为Execute =“ deferred”并将其安排在=“ InstallFiles”之后时,CustomActionData已正确设置并且可用于CustomAction,但安装失败,并出现“找不到文件”异常。计划的Before =“ InstallFinalize”失败,但有相同的例外。
Session.Log:
Calling custom action CustomActions!CustomActions.CustomAction.UpdateJsonAppSettings
Session.CustomActionData.Count(): 2
key: connectionString, value: Data Source=localhost\SQLEXPRESS;Initial Catalog=DB;User Id=dbuser;Password=dbuserpassword;MultipleActiveResultSets=true;App=EntityFramework
key: filepath, value: C:\inetpub\wwwroot\ServiceApi\appsettings.json
UpdateJsonAppSettings() returned: NotExecuted; _result: File not found:C:\inetpub\wwwroot\ServiceApi\appsettings.json; filepath: C:\inetpub\wwwroot\ServiceApi\appsettings.json; connectionString: Data Source=localhost\SQLEXPRESS;Initial Catalog=DB;User Id=dbuser;Password=dbuserpassword;MultipleActiveResultSets=true;App=EntityFramework
这看起来像Catch-22的情况。非常感谢您的帮助。
PS-出于某种原因,我的原始帖子最终出现在META-StackExchange上了?
答案 0 :(得分:0)
非常感谢Stein的建议-它最终使我找到了解决方案,尽管这绝非易事。我不确定如何提出这种解释以及如何将您的评论设为正确答案。
我发现的是,设置CustomDataAction元素(它在CustomAction中定义了appsettings.json的路径)在发送到session.Log时可以正确显示,但实际上并没有进行设置(请参见在线警告)
[CustomAction]
public static ActionResult SetCustomActionData(Session session)
{
CustomActionData _data = new CustomActionData();
// ..escape single ';'
string _connectionString = session["CONNECTION_STRING"];
_connectionString.Replace(";", ";;");
_data["connectionString"] = _connectionString;
// ..correctly output in install log
session.Log(string.Format("SetCustomActionData() setting _connectionString: {0}", _connectionString));
// Property set to [#appSettings] in Product.wxs
string _filePath = session["PATH_TO_APPSETTINGS_JSON"];
_data["filepath"] = _filePath;
// ..correctly output in install log
session.Log(string.Format("SetCustomActionData() setting _filepath: {0}", _filePath));
// ..set the CustomActionData programmatically
session["UpdateJsonAppSettings"] = _data.ToString();
return ActionResult.Success;
}
Install.log:
[SNIP]
SetCustomActionData() setting _connectionString: 'Data Source=localhost\SQLEXPRESS;;Initial Catalog=...'
SetCustomActionData() setting _filepath: 'C:\inetpub\wwwroot\UServiceApi\appsettings.json'
[CustomAction]
public static ActionResult UpdateJsonAppSettings(Session session)
{
// ..as per Stein's suggestion
MessageBox.Show("Attach run32dll.dll now");
// ..correctly output to log (i.e. 2)
session.Log(string.Format("Session.CustomActionData.Count(): {0}", session.CustomActionData.Count));
// ..correctly output two key/value pairs to log
foreach(string _key in session.CustomActionData.Keys)
session.Log(string.Format("key: {0}, value: {1}", _key, session.CustomActionData[_key]));
string _connectionString = session.CustomActionData["connectionString"];
string _pathToAppSettings = session.CustomActionData["filepath"];
// WARNING: _pathToAppSettings has reverted to the literal "[#appSettings]" - which of course triggers File not found Exception.
ActionResult _retVal = UpdateJsonConnectionString(_connectionString, _pathToAppSettings, out string _result);
// ..log failure
if (_retVal != ActionResult.Success)
session.Log(string.Format("UpdateJsonAppSettings() returned: {0}; _result: {1}; filepath: {2}; connectionString: {3}",
_retVal, _result, _pathToAppSettings, _connectionString));
return _retVal;
}
Install.log:
[SNIP]
key: connectionString, value: Data Source=localhost\SQLEXPRESS;Initial Catalog=...
key: filepath, value: C:\inetpub\wwwroot\UServiceApi\appsettings.json
[SNIP]
UpdateJsonAppSettings() returned: NotExecuted; _result: File not found:C:\inetpub\wwwroot\ServiceApi\appsettings.json...
我尝试了Properties和CustomAction实现的多种不同组合,但最终发现,正确设置CustomActionData包含“真实”文件路径元素的唯一方法是将其设置为Product中的Type 51(我认为)CustomAction .wxs。我尝试过的其他组合都没有用。
Product.wxs中的工作代码段:
<Property Id="PATH_TO_APPSETTINGS_JSON" Value="[#appSettings]" />
<CustomAction Id="SetCustomActionData"
Return="check"
Property="UpdateJsonAppSettings"
Value="connectionString=[CONNECTION_STRING_FORMATTED];filepath=[#appSettings]" />// NOTE: cannot use filepath=[PATH_TO_APPSETTINGS_JSON] here
结论:一个可能的WiX错误-日志没有说明真相(它选择性地“翻译”了CustomActionData元素,而编译的安装程序代码实际上使用了不同的值)。