将 ConnectionString 传递给 WiX 安装程序中的自定义操作(转义分号) [英] Pass ConnectionString to Custom Action in WiX Installer (escape semicolon)

查看:16
本文介绍了将 ConnectionString 传递给 WiX 安装程序中的自定义操作(转义分号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用 WiX 的第一个项目.
我正在为 Windows 服务创建安装程序,在安装过程中,我需要收集该服务将使用的一些配置数据.这包括到数据库的连接字符串.

This is my first project with WiX.
I'm creating an installer for Windows Service and during the installation, I need to collect some configuration data that the service will use. This includes the connection string to the database.

<Binary Id="CustomActionBinary" SourceFile="$(var.ServiceSetupActions.TargetDir)$(var.ServiceSetupActions.TargetName).CA.dll"/>
  <CustomAction
    Id="ServiceSetupActions"
    BinaryKey="CustomActionBinary"
    DllEntry="SaveCompanySettings"
    Execute="deferred"
    Return="check"
    Impersonate="no" />
  <CustomAction
    Id="SetupCustomProperties"
    Property="ServiceSetupActions"
    Value="DBTYPE=[DBTYPE];CONNECTIONSTRING=[CONNECTIONSTRING];INSTALLFOLDER=[INSTALLFOLDER]"/>

<InstallExecuteSequence>
  <Custom Action="SetupCustomProperties" Before="ServiceSetupActions" />
  <Custom Action="ServiceSetupActions" After="InstallFiles">NOT Installed</Custom>
</InstallExecuteSequence>

问题在于 WiX 使用分号作为数据分隔符 (https://github.com/wixtoolset/Dtf/blob/09dc7b4e4494182c0906bf5492f12e09c918444f/src/WixToolset.Dtf.WindowsInstaller/customactiondata.cs>),就是输入的连接字符串#L32

Problem is that WiX is using a semicolon as a data separator (https://github.com/wixtoolset/Dtf/blob/09dc7b4e4494182c0906bf5492f12e09c918444f/src/WixToolset.Dtf.WindowsInstaller/customactiondata.cs#L32), so the connection string that is entered during the setup is incorrectly deserialized in my custom action.

问题是:我如何正确地将包含分号的字符串传递给自定义操作(使用 session.CustomActionData),以免其格式错误.

The question is: How can I correctly pass a string that contains a semicolon to custom action (using a session.CustomActionData) so it will not be misformed.

另一种选择是完整的 SQL 连接对话框,它会询问服务器、数据库、用户名和密码,但该服务可以处理 PostgresSQL 和 MS SQL,有时连接字符串可能包含一些修改.

The alternative is a full SQL connection dialog that will ask for server, database, user name, and password, but the service can handle PostgresSQL and MS SQL and sometimes the connection strings may contain some modifications.

推荐答案

请检查此示例,看看它是否适合您:https://github.com/glytzhkof/WiXDeferredModeSample

Please check this sample and see if it works for you: https://github.com/glytzhkof/WiXDeferredModeSample

这是示例的另一个版本,这个版本使用 DTF CustomActionData 类来实现更多自动魔术":https://github.com/glytzhkof/WiXDeferredModeSampleDTF - 它将为您安排自定义操作,您可以在延迟模式下访问属性.已完成有限测试.

Here is another version of the sample, this one uses the DTF CustomActionData class for more "auto-magic": https://github.com/glytzhkof/WiXDeferredModeSampleDTF - it will schedule the custom actions for you and you can just access the properties in deferred mode. Limited testing done.

  1. 在此源代码行中设置升级 GUID 上面写着 "PUT-GUID-HERE" - 您可以在此处创建 GUID.

更改属性 MYPROPERTY 以包含分号 ;.在此 WiX 源代码行上执行此操作.如果您愿意,您可以使用以下示例文本(当然可以使用其他任何方法):

Change the property MYPROPERTY to contain semicolons ;. Do so at this WiX source line. You can use the below sample text if you like (anything else will do of course):

<Property Id="MYPROPERTY" Hidden="yes" Secure="yes">Test;Test1;Test2</Property>

  • 编译并进行测试运行.将显示一个消息框,其中应包含您在源文件中指定的完整字符串.

  • Compile and do a test run. There will be a message box showing and it should contain the full string you specified in the source file.


    如果您想在发送到延迟模式的字符串中组合多个属性值,您有几个选项.最简单的方法是使用您需要的值设置多个属性,然后将它们组合成一个发送到延迟模式的字符串.您可以通过多种方式设置属性:对话框、命令行、输入框等...:


    If you want to combine several property values inside the string you send to deferred mode you have a few options. The simplest is to set several properties with the values you need and then combine them in a string sent to deferred mode. You can set the properties in several ways: dialog, command line, input boxes, etc...:

    • MYCOMPANY =某些公司"
    • MYDATABASE = "TheDatabaseName"
    • 等等...

    然后在立即模式自定义操作中调用 Session.Format 来解析字符串中的值.像这样:

    Then you call Session.Format in an immediate mode custom action to resolve values in your string. Something like this:

    COMPANYID=[COMPANYID];DBTYPE=[DBTYPE];CONNECTIONSTRING=[CONNECTIONSTRING];INSTALLFOLDER=[INSTALLFOLDER]

    COMPANYID=[COMPANYID];DBTYPE=[DBTYPE];CONNECTIONSTRING=[CONNECTIONSTRING];INSTALLFOLDER=[INSTALLFOLDER]

    var CAdata = Session.Format("MYCOMPANY=[MYCOMPANY];MYDB=[MYDB];MYDBTYPE=[MYDBTYPE]");
    session["MYPROPERTYSENTTODEFERREDMODE"] = CAdata;
    

    您还需要一个 Type 51 CA 来将该字符串实际发送到延迟模式自定义操作.您可以在上面的示例中看到这是如何完成的.

    You also need a Type 51 CA to actually send that string to the deferred mode custom action. You see how that is done in the sample above.

    然后你在延迟模式自定义操作中检索MYPROPERTYSENTTODEFERREDMODE的字符串值,你应该可以直接使用它吗?

    Then you retrieve the string value of MYPROPERTYSENTTODEFERREDMODE in a deferred mode custom action and you should be able to use it directly?

    链接:

    这篇关于将 ConnectionString 传递给 WiX 安装程序中的自定义操作(转义分号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆