【问题标题】:Editing Web.Config Connection string settings with Wix使用 Wix 编辑 Web.Config 连接字符串设置
【发布时间】:2013-04-24 11:41:16
【问题描述】:

我目前正在尝试修改我的 Wix(V3.5) 安装程序以编辑我要安装的 .NET 应用程序的 Web.config 设置。这对于普通的 ASP.NET 应用程序来说很好,但现在我试图将我的 Wix 设置项目应用于实体框架 .NET 应用程序,您可能知道该应用程序具有更复杂的连接字符串设置,带有模型 .csdl 和 .ssdl 设置。

因此,如果我的 web.config 连接字符串设置看起来像这样:(其中 [DBSERVER] 和 [DBNAME] 是从对话框中检索的属性)

  <connectionStrings>
   <add name="SSITacticalSolutionEntities" connectionString="metadata=res://*/Model.TacticalSolutionModel.csdl|res://*/Model.TacticalSolutionModel.ssdl|res://*/Model.TacticalSolutionModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=sd-sql2008r2;Initial Catalog=SsiTacticalSolution1.2.4;Integrated Security=True;MultipleActiveResultSets=True&quot; />
  </connectionStrings>

然后我在我的 Product.Wsx 文件中编辑我的 Web.config,如下所示:

   <util:XmlFile Id="ModifyConnectionString" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config"
                  ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" Name="connectionString"
                  Value="Data Source=[DBSERVER];Initial Catalog=[DBNAME];Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True&quot;"   Sequence="5"/>

我得到一个这样的连接字符串:

  <connectionStrings>
      <add name="SSITacticalSolutionEntities" connectionString="Data Source=sd-sql2008r2;Initial Catalog=SsiTacticalSolution1.2.4;Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True&quot;"/>
  </connectionStrings>

这当然是有道理的,因为我要求它用我在值中定义的内容替换当前的连接字符串属性。

但我在这里真正需要的是编辑我的连接字符串的特定部分并保留其余部分(我可以在这里使用某种替换操作),即保留我所有的模型设置,只需根据需要替换数据库服务器和名称等。我以前用 Visual Studio 安装程序来做这个没问题,而且很容易使用。

所以我的问题是可以使用 util.XMLFile 或 util:XmlConfig 来完成吗?我都试过了,都没有运气。

或者这不能用 util.XMLFile 做,我必须在 CustomAction 中做这个吗? 任何想法都会有很大帮助,在此先感谢...

【问题讨论】:

    标签: .net entity-framework wix wix3.5


    【解决方案1】:

    最后我得到了这个工作,最后我没有为这个特定的设置使用自定义操作,我使用我的本地化文件中设置的变量。

    我这样做是因为它会是开发人员而不是用户知道模型名称和实体名称(不是通过安装对话框的用户,他们不会知道此信息),所以我有一个本地化文件其中有不同的属性,例如产品名称等,因此我添加了模型名称并将名称添加到其中。我从对话框中获得的所有其他内容,由用户输入:即数据库名称、虚拟目录、模拟用户等...

    如果它对任何人有帮助,这就是我最终为我的 web.config 提出的内容;这是我的 product.wxs 中处理此问题的部分。如您所见,我在顶部有一个连接字符串属性,其中 loc.ModelName 的占位符是在我的本地化文件中设置的:

        <Property Id="CONNECTION_STRING"
      Value="metadata=res://*/Model.!(loc.ModelName).csdl|res://*/Model.!(loc.ModelName).ssdl|res://*/Model.!(loc.ModelName).msl;provider=System.Data.SqlClient;provider connection string=&quot;"/>
    
    <!-- The root of the installer. -->
    <Directory Id='TARGETDIR' Name='SourceDir'>
    
      <!-- Install into the inetpub/wwwroot directory -->
      <Directory Id="IISMain" Name='inetpub'>
        <Directory Id="WWWMain" Name='wwwroot' ComponentGuidGenerationSeed='C38ED13E-E1E3-40DB-B1FA-39400C6B2BC4'>
    
    
          <Directory Id='INSTALLLOCATION' Name="!(loc.ProductName)">
    
            <!-- The component to define the Virtual Directory.-->
            <Component Id="WebVirtualDirComponent"
                       Guid="D814F88F-6E0C-4365-A411-2F9807522C3D">
    
              <!-- WebVirtualDir: The virtual directory we are installing. -->
              <!-- Alias:         Alias attribute is the name that we will see in IIS.-->
              <!-- Directory:     The Directory attribute is the "Physical Path" property in
                                IIS and needs to tie to the ID specified above as the install location. -->
              <!-- WebSite:       The WebSite attribute ties to a <WebSite> element in the 
                                setup file(see below). As this is an example of installing into the 
                                "Default Web Site" so that element is not under a component.-->
              <iis:WebVirtualDir Id="VDir" Alias="[VIRTUALDIRECTORYVALUE]"
                                 Directory="INSTALLLOCATION"
                                  WebSite="DefaultWebSite">
    
                <!-- This turns the Virtual Directory into a web application. -->
                <iis:WebApplication Id="MyWebAppApplication"
                                   Name="[VIRTUALDIRECTORYVALUE]" WebAppPool="AppPool"/>
    
                <iis:WebDirProperties Id="WebSite_Properties" AnonymousAccess="no"
                                      WindowsAuthentication="yes" DefaultDocuments="!(loc.DefaultDocument)"
                                      Script="yes" Read="yes" />
    
              </iis:WebVirtualDir>
              <CreateFolder/>
              <RemoveFolder Id= "GuidFolders" On= "uninstall"/>
          </Component>
    
            <!-- Components - this decides what we want to incude in our install
            Here we will alter our web.config for Impersonation , debug to false and connection string. -->
            <Component Id="Web.config" Guid="2ED81B77-F153-4003-9006-4770D789D4B6">
    
              <!--install our web.config file , this isnt part of our initial MSBUILD-->
             <File Id="Web.config" Name="Web.config" Source="$(var.SolutionDir)!(loc.WebApplicationProjectName)\Web.config" DiskId="1" KeyPath="yes" />
    
              <!--Modify our web.config - here we need to add Identity impersonation , changes session settings , add connection string settings and set debug setting-->
              <!--Ensure that the identity setting exists--> 
              <util:XmlFile Id="system.webidentity" 
                            File="[INSTALLLOCATION]Web.config" 
                            Action="createElement" 
                            ElementPath="/configuration/system.web" 
                            Name="identity" 
                            SelectionLanguage="XPath"
                            Sequence="1" />
    
              <util:XmlFile Id="system.webIdentityAttribute" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/identity" 
                            Name="impersonate" 
                            Value="true" 
                            SelectionLanguage="XPath"
                            Sequence="2" />
    
              <util:XmlFile Id="system.webIdentityAttribute2" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/identity" 
                            Name="password" 
                            Value="[IMPERSONATIONUSERPASSWORD]" 
                            SelectionLanguage="XPath"
                            Sequence="3" />
    
              <util:XmlFile Id="system.webIdentityAttribute3" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/identity" 
                            Name="userName" 
                            Value="[IMPERSONATIONUSER]"
                            SelectionLanguage="XPath"
                            Sequence="4" />
    
              <util:XmlFile Id="ModifyConnectionString" 
                            Action="setValue" 
                            Permanent="yes" 
                            File="[INSTALLLOCATION]Web.config"
                            ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" 
                            Name="connectionString"
                            Value="[CONNECTION_STRING]Data Source=[DBSERVER];Initial Catalog=[DBNAME];Integrated Security=True;MultipleActiveResultSets=True&quot;" 
                            SelectionLanguage="XPath"
                            Sequence="5"/>
    
              <!--<authentication mode="Forms">-->
              <util:XmlFile Id="AuthenticationModeWindows" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config"
                            ElementPath="/configuration/system.web/authentication" 
                            Name="mode" 
                            Value="Windows" 
                            Sequence="6" />
    
              <!--Switch off debug-->
              <util:XmlConfig Sequence="7" 
                              Id="SwitchOffDebug" 
                              File="[INSTALLLOCATION]\web.config" 
                              Action="create" On="install" 
                              Node="value" 
                              ElementPath="/configuration/system.web/compilation" 
                              Name="debug" 
                              Value="false" />
    
    
               <!--Session configuration  <sessionState mode="InProc" timeout="15" />-->
              <util:XmlFile Id="system.websessionState" 
                            File="[INSTALLLOCATION]Web.config" 
                            Action="createElement" 
                            ElementPath="/configuration/system.web" 
                            Name="sessionState" 
                            Sequence="8" />
    
              <util:XmlFile Id="system.websessionStateAttribute" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/sessionState" 
                            Name="mode" Value="InProc" 
                            Sequence="9" />
    
              <util:XmlFile Id="system.websessionStateAttribute2" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/sessionState" 
                            Name="timeout" 
                            Value="15" 
                            Sequence="10" />
    
              <util:XmlFile Id="system.websessionStateAttribute3" 
                            Action="setValue" 
                            File="[INSTALLLOCATION]Web.config" 
                            ElementPath="/configuration/system.web/sessionState" 
                            Name="cookieName" 
                            Value="[VIRTUALDIRECTORYVALUE]" 
                            Sequence="11" />
            </Component>
    
    <iis:WebSite Id='DefaultWebSite'
                 Description='Default Web Site'
                 Directory='INSTALLLOCATION' SiteId ='[WEBSITEVALUE]' >
    
      <iis:WebAddress Id="AllUnassigned" Port="80" />
    </iis:WebSite>
    <iis:WebAppPool Id="AppPool" Name="[APPPOOLVALUE]" />
    
    <CustomAction Id="MapVirtualDirectory"  Directory="INSTALLLOCATION"  Return="asyncNoWait"
                  ExeCommand='[ASPNETREGIIS] -norestart -s "W3SVC/[WEBSITEVALUE]/ROOT/[VIRTUALDIRECTORYVALUE]"' />
    
    <InstallExecuteSequence>
      <Custom Action="MapVirtualDirectory" After="InstallFinalize"    >ASPNETREGIIS AND NOT Installed</Custom>
    </InstallExecuteSequence>
    
    <CustomAction Id="GetIISWebSites" BinaryKey="IisManager" DllEntry="GetWebSites" Execute="immediate" Return="check" />
    <CustomAction Id="GetIISAppPools" BinaryKey="IisManager" DllEntry="GetAppPools" Execute="immediate" Return="check" />
    
    <InstallUISequence>
      <Custom Action="GetIISWebSites" After="CostFinalize" Overridable="yes">NOT Installed</Custom>
      <Custom Action="GetIISAppPools" After="CostFinalize" Overridable="yes">NOT Installed</Custom>
    </InstallUISequence>
    
    <Feature Id='ApplicationFeatures' Title="!(loc.ProductName)" Level='1'>
      <ComponentRef Id='WebVirtualDirComponent' />
      <ComponentGroupRef  Id="MyWebApp_Project" />
      <ComponentRef Id="Web.config" />
    
    </Feature>
    
    <!-- Specify UI -->
    <Property Id="WIXUI_INSTALLDIR">INSTALLLOCATION</Property>
    <UIRef Id="MyCustomUI"/>  
    

    这是我的本地化文件:

    <?xml version="1.0" encoding="utf-8"?>
    <WixLocalization Culture="en-us" xmlns="http://schemas.microsoft.com/wix/2006/localization">
    
      <!--application settings-->
      <String Id="LANG">1033</String>
      <String Id="ProductName">MyTestWebSite</String>
      <String Id="ProductVersion">1.0.0.0</String>
      <String Id="CompanyName">MyCompanyName</String>
      <String Id="DefaultDocument">Default.aspx</String>
      <String Id="WebApplicationProjectName">MyWebApp</String>
    
    
      <!--database settings-->
      <String Id="EntityName">MyEntities</String>
      <String Id="ModelName">MyModel</String>
    
    </WixLocalization>
    

    【讨论】:

    • Necro 但是...在示例中用“PUT-GUID-HERE”替换您的真实 GUID 是更好的做法。
    • 感谢 Izzy 的提示
    • 您好,GUID 对程序有何风险?任何人都可以更改项目 GUID 吗??
    【解决方案2】:

    XmlFileXmlConfig 都在原子级别写入属性。要获得您想要的行为,您需要编写一个即时自定义操作来读取 XML 文件并将结果存储在 Property 中。然后按照您认为合适的方式操作 Property(如果操作很复杂,您可能需要在自定义操作中执行此操作),然后让 XmlFileXmlConfig 将整个操作值写回。

    此方法将需要您的代码中最简单的自定义操作集,允许XmlFileXmlConfig 完成繁重的工作并处理回滚和所有这些事情。只需对Property 幂等进行修改即可。

    祝你好运!

    【讨论】:

    • 谢谢 Rob。我目前没有通过 msbuild 进程添加我的 web.config,而是通过我的 product.wsx 中的一个组件单独添加它。原因是,以及操作连接字符串设置,我还有其他几个
    • 是的,最后您必须将结果输入到 Property 或解析为 XmlFile 中的一行的一系列属性。我确信这是可行的,魔鬼就在细节中。 :)
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 2015-08-30
    • 2010-11-24
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多