Dot-Net

使用 Wix 編輯 Web.Config 連接字元串設置

  • July 11, 2014

我目前正在嘗試修改我的 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="Data Source=sd-sql2008r2;Initial Catalog=SsiTacticalSolution1.2.4;Integrated Security=True;MultipleActiveResultSets=True" />
 </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""   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""/>
 </connectionStrings>

這當然是有道理的,因為我要求它用我在值中定義的內容替換目前的連接字元串屬性。

但我在這裡真正需要的是編輯我的連接字元串的特定部分並保留其餘部分(我可以在這裡使用某種替換操作),即保留我所有的模型設置並替換數據庫伺服器和名稱等我需要。我以前用 Visual Studio 安裝程序來做這件事沒問題,而且它很容易使用。

所以我的問題是這可以使用 util.XMLFile 或者 util:XmlConfig 來完成嗎?我都試過了,沒有任何運氣。

或者這不可能用 util.XMLFile 做,我必須在 CustomAction 中做這個嗎?任何想法都會有很大幫助,在此先感謝…

我最終得到了這個工作,最後我沒有為這個特定設置使用自定義操作,我使用我的本地化文件中設置的變數。

我這樣做是因為它將是開發人員而不是知道模型名稱和實體名稱的使用者(不是通過安裝對話框的使用者,他們不會知道此資訊),所以我有一個具有不同屬性的本地化文件它像產品名稱等,所以我添加了一個模型名稱和實體名稱。我從對話框中得到的所有其他內容,由使用者輸入:即數據庫名稱、虛擬目錄、模擬使用者等…

如果它對任何人有幫助,這就是我最終為我的 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=""/>

<!-- 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"" 
                       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>

引用自:https://stackoverflow.com/questions/16191073