![](/img/trans.png)
[英]How to include a GAC'd dll in a Wix managed CustomAction assembly?
[英]Wix CustomAction dll
我有一个使用CAQuietExec的自定义操作,在某些情况下会失败-唯一的错误消息出现在日志文件中,这对我作为开发人员来说是个好消息,而对最终用户来说却毫无用处。 我的目标是在回滚安装之前捕获失败的操作并预设标准错误消息。
为此,根据我的研究,我决定需要编写自己的自定义操作dll,因此开始学习本教程 。 在此阶段,我的dll仅记录一条测试消息,并尝试将错误消息传递回对话框屏幕。 但是,当我编译并运行msi时,什么也没有发生-没有日志记录,也没有错误消息。 查看Orca中的msi,对我来说看起来不错-但如您在下面看到的,如果测试dll实际上运行并且没有运行,则测试dll应该导致安装立即中止。
所以我的问题是:我要以最好的方式向用户提供反馈吗? 如果是这样,为什么我的自定义操作无法执行?
谢谢
CustomAction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Collections.ObjectModel;
using System.Management.Automation;
namespace MyCustomActions
{
public class CustomActions
{
[CustomAction]
public static ActionResult CustomAction1(Session session)
{
session.Log("Begin CustomAction1");
DisplayMSIError(session, "A test message");
return ActionResult.Failure;
}
static void DisplayMSIError(Session session, String msg)
{
var r = new Record();
r.SetString(0, msg);
session.Message(InstallMessage.Error, r);
}
}
}
产品.wxs
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="MyPackage" Language="1033" Version="0.0.0.1" Manufacturer="Acme" UpgradeCode="GUID">
<Package InstallerVersion="300" Compressed="yes" InstallScope="perMachine" />
<Binary Id="MyCustomActions.CA.dll" src="MyCustomActions\MyCustomActions.CA.dll" />
<CustomAction Id="MyCustomActionsTest"
Return="check"
Execute="immediate"
BinaryKey="MyCustomActions.CA.dll"
DllEntry="CustomAction1" />
<InstallExecuteSequence>
<Custom Action="MyCustomActionsTest" After="LaunchConditions" />
...
</InstallExecuteSequence>
</Product>
</Wix>
gh,我在这上面浪费了时间:但是对于遇到相同问题的任何人,我都会通过自定义操作发布解决我的问题的答案-希望它可以节省您的时间和沮丧的时间。
我很早就将测试动作放在序列中,以为我会看到错误消息对话框。 因此,我实际上并没有尝试完成安装-当出现欢迎对话框时,我认为我的操作没有运行。 但是,它必须已运行,因为当您实际选择功能并允许安装进行时,随后会出现错误消息。
不过,如果任何人都可以提供某种见解,以了解这是否是从CAQuietExec正常运行的动作中提供一些用户反馈的最佳方法,将不胜感激。
您如何看待该解决方案:
Record record = new Record();
record.FormatString = string.Format("Something has gone wrong!");
session.Message(
InstallMessage.Error | (InstallMessage) ( MessageBoxIcon.Error ) |
(InstallMessage) MessageBoxButtons.OK,
record );
从这个答案
另一种方法-创建自己的UI并根据需要显示任何消息。 小例子:
自定义操作:
[CustomAction]
public static ActionResult CheckSqlSessionState(Session session)
{
try
{
session.SendMessage(InstallMessage.Info, "Check Sql Session State");
return ActionResult.Success;
}
catch (Exception exception)
{
session.SendMessage(InstallMessage.Error,
string.Format("Error during the cheking sesssion state. {0}", exception.Message));
return ActionResult.Failure;
}
}
注册:
<CustomAction Id="WiXSetup.CustomAction.CheckSqlSessionState"
BinaryKey="WiXSetup.CustomActions.dll"
DllEntry="CheckSqlSessionState" Execute="deferred" />
<CustomAction Id="SetCheckSqlSessionStateDataValue"
Return="check"
Property="WiXSetup.CustomAction.CheckSqlSessionState" />
<InstallExecuteSequence>
<Custom Action="SetCheckSqlSessionStateDataValue" Before="WiXSetup.CustomAction.CheckSqlSessionState">NOT Installed</Custom>
<Custom Action="WiXSetup.CustomAction.CheckSqlSessionState" After="WiXSetup.CustomAction.UpdateHomePageUrls">NOT Installed</Custom>
</InstallExecuteSequence>
CSharp用户界面:
using Microsoft.Deployment.WindowsInstaller;
private ExternalUIRecordHandler recordHandler;
// ...
IntPtr parent = IntPtr.Zero;
recordHandler = RecordHandler;
private MessageResult RecordHandler(InstallMessage messageType, Record messageRecord, MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton)
{
switch (messageType)
{
case InstallMessage.Info:
log.Info(message);
return MessageResult.OK;
case InstallMessage.Initialize:
log.Debug(message);
return MessageResult.OK;
case InstallMessage.ActionData:
log.Debug(message);
return MessageResult.OK;
case InstallMessage.ActionStart:
log.Debug(message);
return MessageResult.OK;
case InstallMessage.CommonData:
return MessageResult.OK;
case InstallMessage.Error:
log.Error(message);
return MessageResult.OK;
case InstallMessage.FatalExit:
log.Fatal(message);
return MessageResult.OK;
case InstallMessage.FilesInUse:
return MessageResult.No;
case InstallMessage.OutOfDiskSpace:
break;
case InstallMessage.Progress:
return MessageResult.OK;
case InstallMessage.ResolveSource:
return MessageResult.No;
case InstallMessage.ShowDialog:
return MessageResult.OK;
case InstallMessage.Terminate:
return MessageResult.OK;
case InstallMessage.User:
return MessageResult.OK;
case InstallMessage.Warning:
log.Warn(message);
return MessageResult.OK;
}
return MessageResult.No;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.