WiX 自定义操作以按钮开始
Posted
技术标签:
【中文标题】WiX 自定义操作以按钮开始【英文标题】:WiX Custom Action start with Button 【发布时间】:2015-10-28 16:02:20 【问题描述】:我有一个自定义操作来检查 SQL 连接。 现在,它应该可以与控制按钮一起使用,但这不起作用。
自定义操作在没有按钮的情况下也能很好地工作: 这里是 wxs 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Property Id="SERVERNAME" Value="MSSQL2008R2" />
<Property Id="DATABASENAME" Value="MyDatabase" />
<Property Id="USERNAME" Value="admin" />
<Property Id="PASSWORD" Value="mypassword" />
<Binary Id="CA_SQLTestDLL" SourceFile="$(var.CA_SQLConnectionTest.TargetDir)CA_SQLConnectionTest.CA.dll" />
<CustomAction Id="SQL_Test"
BinaryKey="CA_SQLTestDLL"
DllEntry="ConnectionTest"
Execute="deferred"
Return="check" />
<SetProperty Id="SQL_Test" Value="SERVERNAME=[SERVERNAME];DATABASENAME=[DATABASENAME];USERNAME=[USERNAME];PASSWORD=[PASSWORD]" Sequence="execute" Before="SQL_Test" />
<InstallExecuteSequence>
<Custom Action="SQL_Test" After="InstallInitialize" />
</InstallExecuteSequence>
<UI>
<Dialog Id="SQLServerConnectionTestDlg" Width="370" Height="270" Title="SQL Server connection test">
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)" />
<Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)" />
<Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)">
<Publish Event="SpawnDialog" Value="CancelDlg">1</Publish>
</Control>
</Dialog>
</UI>
这是我的 CustomAction 类:
Imports System.Data
Imports System.Data.SqlClient
Public Class CustomActions
<CustomAction()> _
Public Shared Function ConnectionTest(ByVal session As Session) As ActionResult
session.Log("############## Begin CUSTOMACTION ##############")
Dim userName As String
Dim password As String
Dim serverName As String
Dim dataBase As String
serverName = session.CustomActionData("SERVERNAME")
dataBase = session.CustomActionData("DATABASENAME")
userName = session.CustomActionData("USERNAME")
password = session.CustomActionData("PASSWORD")
Dim SqlConn As New SqlConnection
Dim SqlConnStr As String = "Data Source=" + serverName + ";Database=" + dataBase + ";Persist Security Info=True;User ID=" + userName + ";Password=" + password
If SqlConn.State = ConnectionState.Closed Then
SqlConn.ConnectionString = SqlConnStr
Try
SqlConn.Open()
Catch ex As Exception
Return ActionResult.Failure
End Try
End If
session.Log("### SUCCESSFULL ###")
Return ActionResult.Success
End Function
End Class
安装日志:
Calling custom action CA_SQLConnectionTest!CA_SQLConnectionTest.CustomActions.ConnectionTest
############## Begin CUSTOMACTION ##############
### SUCCESSFULL ###
现在,我想用一个按钮启动自定义操作。 所以我必须将 Execute="deferred" 更改为 Execute="immediate" 并添加一个按钮:
<Control Id="TestConn" Type="PushButton" X="265" Y="205" Width="70" Height="18" Text="&Test Connection">
<Publish Event="DoAction" Value="SQL_Test">1</Publish>
<Publish Property="ERRORMSG" Value="ConnectionTest">ACCEPTED = "1"</Publish>
<Publish Event="SpawnDialog" Value="InvalidDBConnDlg">ACCEPTED = "0"</Publish>
</Control>
<Dialog Id="InvalidDBConnDlg" Width="260" Height="120" Title="MyTester">
<Control Id="OK" Type="PushButton" X="102" Y="90" Width="56" Height="17" Default="yes" Cancel="yes" Text="OK">
<Publish Event="EndDialog" Value="Exit" />
</Control>
<Control Id="Text" Type="Text" X="48" Y="22" Width="194" Height="60" Text="FAILED" />
<Control Id="Icon" Type="Icon" X="15" Y="15" Width="24" Height="24" ToolTip="Information icon" FixedSize="yes" IconSize="32" Text="WixUI_Ico_Info" />
</Dialog>
现在,当我按下按钮时,设置将取消,并且我在日志文件中有一个致命错误:
Action 16:43:20: SQL_Test.
Action start 16:43:20: SQL_Test.
MSI (c) (5C:5C) [16:43:20:236]: Invoking remote custom action. DLL: C:\Users\LOC~1.CRE\AppData\Local\Temp\MSIC08C.tmp, Entrypoint: ConnectionTest
Action ended 16:43:20: SQL_Test. Return value 3.
MSI (c) (5C:20) [16:43:20:404]: Note: 1: 2205 2: 3: Error
MSI (c) (5C:20) [16:43:20:404]: Note: 1: 2228 2: 3: Error 4: SELECT `Message` FROM `Error` WHERE `Error` = 2896
DEBUG: Error 2896: Executing action SQL_Test failed.
The installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 2896. The arguments are: SQL_Test, ,
Action ended 16:43:20: WelcomeDlg. Return value 3.
MSI (c) (5C:54) [16:43:20:405]: Doing action: FatalError
MSI (c) (5C:54) [16:43:20:405]: Note: 1: 2205 2: 3: ActionText
Action 16:43:20: FatalError.
Action start 16:43:20: FatalError.
Action 16:43:20: FatalError. Dialog created
Action ended 16:43:23: FatalError. Return value 2.
Action ended 16:43:23: INSTALL. Return value 3.
MSI (c) (5C:54) [16:43:23:488]: Destroying RemoteAPI object.
MSI (c) (5C:50) [16:43:23:488]: Custom Action Manager thread ending
配置 useLegacyV2RuntimeActivationPolicy 为“真”
我在 Visual Studio 2013 中使用 WIX3.10
我不确定我做错了什么。但我认为这是 InstallExecuteSequence 的问题。
希望有人能帮帮我,谢谢
【问题讨论】:
【参考方案1】:立即执行是正确的做法,因为您不能在 UI 序列中延迟自定义操作(因此您怀疑执行序列不会有问题)。
除此之外,如果代码启动但失败,那么这是一个编码/环境问题。当您处于调试阶段时,您应该只在代码中调用消息框,检查您是否拥有这些值,并且肯定会做更多的事情来消除该 try/catch 的任何异常!您在丢弃错误并返回失败结果的同时询问错误可能是什么。
【讨论】:
【参考方案2】:谢谢,你没看错,不是执行顺序的问题。
我已经缩小了问题的范围。当我不使用 CustomActionData 时,将使用自定义操作
userName = session.CustomActionData("USERNAME")
password = session.CustomActionData("PASSWORD")
...
为什么我不能不传递值?
更新:
命令在这里不起作用:
session.CustomActionData("USERNAME")
使用以下命令可以正常工作:
session("USERNAME")
【讨论】:
【参考方案3】:CustomActionData 无法通过即时自定义操作访问。
但您可以使用会话直接访问属性。
试试这个
userName = session["USERNAME"];
password = session["PASSWORD"];
【讨论】:
以上是关于WiX 自定义操作以按钮开始的主要内容,如果未能解决你的问题,请参考以下文章
如何在关闭对话框的 Wix 安装程序中停止自定义操作中的进度条