修改数据库时,WiX 工具集自定义操作失败

本文关键字:自定义 操作 失败 工具集 WiX 数据库 修改 | 更新日期: 2023-09-27 18:37:08

我一直在疯狂地试图弄清楚为什么视图没有更新。从我在网上看到的所有帖子中,我认为我做得对,但我不断收到 Null 错误。如果我尝试使用会话。Database.FilePath 以加载数据库的方式,它说 FilePath 为空。视图本身返回虚拟行,因此数据库位于其中。这太奇怪了。

以下是属性

<Property Id="IIS_SITE" />

二进制和自定义操作

<CustomAction Id="UpdateComboBoxes" DllEntry="UpdateComboBoxes" BinaryKey="UpdateComboBoxes" Execute="immediate" Return="check" />
<Binary Id="UpdateComboBoxes" SourceFile="..'ProjectName.CustomActions'bin'Release'ProjectName.CustomActions.CA.dll"/>

安装 UI 序列

<InstallUISequence>
  <Custom Action="UpdateComboBoxes" Before="CostFinalize"></Custom>
</InstallUISequence>

控件

   <Control Id="IisSite" Type="ComboBox" Sorted="yes" ComboList="yes" Property="IIS_SITE" X="45" Y="85" Width="220" Height="18" >
      <ComboBox Property="IIS_SITE" >
        <ListItem Text="Dummy" Value="Dummy"/>
      </ComboBox>
    </Control>

自定义操作

     [CustomAction]
    public static ActionResult UpdateComboBoxes(Session session)
    {
        session.Log("Begin Custom Action UpdateComboBoxes");
        try
        {
            session.Log(string.Format("Database Location is: {0}", session.Database.FilePath));
            var database = session.Database;
                using (var view = database.OpenView("SELECT * FROM ComboBox WHERE Property = 'IIS_SITE'"))
                {
                    view.Execute();
                    session.Log("Executed view");
                    var isReadOnly = database.IsReadOnly;
                    session.Log(string.Format("Database is read only: {0}", isReadOnly));
                    session.Log(string.Format("# of rows in ComboBox Table: {0}",
                        view.Database.CountRows("ComboBox", "Property = 'IIS_SITE'")));
                    using (var serverManager = new ServerManager())
                    {
                        session.Log("Accessed Server Manager");
                        var index = 1;
                        var rowIndex = 1;
                        session.Log(string.Format("Going through {0} sites", serverManager.Sites.Count));
                        foreach (var site in serverManager.Sites)
                        {
                            if (!string.IsNullOrEmpty(site.Name))
                            {
                                session.Log(string.Format("Site # {0} {1}", index, site.Name));
                                var record = session.Database.CreateRecord(4);
                                //Property
                                record.SetString(1, "IIS_SITE");
                                //Order
                                record.SetString(2, rowIndex.ToString());
                                //Value
                                record.SetString(3, site.Name);
                                //Text
                                record.SetString(4, site.Name);
                                session.Log(string.Format("Modifying the view for site # {0}", index));
                                view.Modify(ViewModifyMode.InsertTemporary, record);
                            }
                            session.Log("Incrementing index");
                            index++;
                            rowIndex++;
                        }
                    }
                    session.Log("Closing the view");
                }
        }
        catch (Exception e)
        {
            session.Log(string.Format("ERROR in UpdateComboBoxes: {0}", e.Message));
            session.Log(e.StackTrace);
            var inner = e.InnerException;
            if(inner != null)
            {
                session.Log(string.Format("{0}{1}", "'t", inner.Message));
                session.Log(string.Format("{0}{1}", "'t", inner.StackTrace));
            }
            while ((inner = inner.InnerException) != null)
            {
                session.Log(string.Format("{0}{1}", "'t", inner.Message));
                session.Log(string.Format("{0}{1}", "'t", inner.StackTrace));
            }
            return ActionResult.Failure;
        }
        return ActionResult.Success;
    }

我得到的错误:

微星 (c) (B0!F4) [14:46:40:369]: 注意: 1: 2259 2: 3: 4:
更新组合框中的错误:函数在执行过程中失败。 at Microsoft.Deployment.WindowsInstaller.View.Modify(ViewModifyMode mode, Record record) at VideoQuestionInstaller.CustomActions.CustomActions.UpdateComboBoxes(Session session) 自定义操作引发的异常: System.Reflection.TargetInvocationException:调用的目标引发了异常。---> System.NullReferenceException:对象引用未设置为对象的实例。 at VideoQuestionInstaller.CustomActions.CustomActions.UpdateComboBoxes(Session session) ---内部异常堆栈跟踪结束--- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture) at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr)

我查了一下,错误 2259 意味着数据库更新失败,这很清楚,因为错误之前的最后一个日志是:修改站点 # 1 的视图。

有没有人知道我做错了什么,以及为什么 ComboBox 数据库没有更新?

提前感谢!

修改数据库时,WiX 工具集自定义操作失败

关系,我想出了问题所在。这是我修复它的方法:

我添加了另一个隐藏的控件,以便创建组合框表,而无需将假值放入我将要使用的实际组合框中

    <Property Id="HIDDEN_IIS_SITE" />
    <Control Id="DummyComboBox" Hidden="yes" Type="ComboBox" Sorted="yes" ComboList="yes" Property="HIDDEN_IIS_SITE" X="45" Y="85" Width="220" Height="18" >
      <ComboBox Property="HIDDEN_IIS_SITE" >
        <ListItem Text="Dummy" Value="Dummy"/>
      </ComboBox>
    </Control>

然后,我更改了自定义操作,使其获得现有行的实际数量,然后在添加记录时添加到数字中,以便使用正确的订单编号

    [CustomAction]
    public static ActionResult UpdateComboBoxes(Session session)
    {
        session.Log("Begin Custom Action UpdateComboBoxes");
        try
        {
            var database = session.Database;
                using (var view = database.OpenView("SELECT * FROM ComboBox WHERE Property = 'IIS_SITE'"))
                {
                    view.Execute();
                    session.Log("Executed view");
                    var index = view.Database.CountRows("ComboBox", "Property = 'IIS_SITE'");
                    using (var serverManager = new ServerManager())
                    {
                        foreach (var site in serverManager.Sites)
                        {
                            if (!string.IsNullOrEmpty(site.Name))
                            {
                                var record = session.Database.CreateRecord(4);
                                //Property
                                record.SetString(1, "IIS_SITE");
                                //Order
                                record.SetString(2, (++index).ToString());
                                //Value
                                record.SetString(3, site.Name);
                                //Text
                                record.SetString(4, site.Name);
                                view.InsertTemporary(record);
                                session.Log("Inserted new record");
                            }
                        }
                    }
                    session.Log("Closing the view");
                }
        }
        catch (Exception e)
        {
            session.Log(string.Format("ERROR in UpdateComboBoxes: {0}", e.Message));
            session.Log(e.StackTrace);
            var inner = e.InnerException;
            if(inner != null)
            {
                session.Log(string.Format("{0}{1}", "'t", inner.Message));
                session.Log(string.Format("{0}{1}", "'t", inner.StackTrace));
            }
            while (inner != null && (inner = inner.InnerException) != null)
            {
                session.Log(string.Format("{0}{1}", "'t", inner.Message));
                session.Log(string.Format("{0}{1}", "'t", inner.StackTrace));
            }
            return ActionResult.Failure;
        }
        return ActionResult.Success;
    }