使用Wix编辑Web.Config连接字符串设置 [英] Editing Web.Config Connection string settings with Wix

查看:63
本文介绍了使用Wix编辑Web.Config连接字符串设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试修改Wix(V3.5)安装程序,以编辑要安装的.NET应用程序的Web.config设置.这对于普通的ASP.NET应用程序来说很好,但是现在我试图将我的Wix设置项目应用到Entity Framework .NET应用程序,如您所知,该应用程序具有模型.csdl和.ssdl设置的更复杂的连接字符串设置./p>

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

 < connectionStrings><添加名称="SSITacticalSolutionEntities" connectionString ="metadata = res://*/Model.TacticalSolutionModel.csdl | res://*/Model.TacticalSolutionModel.ssdl | res://*/Model.TacticalSolutionModel.msl; provider= System.Data.SqlClient;提供者连接字符串=&数据源= sd-sql2008r2;初始目录= SsiTacticalSolution1.2.4;集成安全性= 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];初始目录= [DBNAME];集成安全性= 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& quot;"/</connectionStrings> 

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

但是我真正需要的是编辑连接字符串的特定部分并保留其余部分(是否可以在此处使用某种替换操作),即保留我所有的模型设置,并根据需要替换数据库服务器和名称等.我以前在Visual Studio安装程序中做到这一点没问题,而且非常易于使用.

所以我的问题是可以使用util.XMLFile还是util:XmlConfig来完成?我都没有运气尝试过.

或者这与util.XMLFile是不可能的,我将不得不在CustomAction中这样做吗?任何想法都会有很大帮助,在此先感谢...

解决方案

最后,我完成了此工作,最后,我没有针对此特定设置使用自定义操作,而是使用了在本地化文件中设置的变量.

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

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

 <属性ID ="CONNECTION_STRING"值="metadata = res://*/Model.!(loc.ModelName).csdl | res://*/Model.!(loc.ModelName).ssdl | res://*/Model.!(loc.ModelName).msl; provider = System.Data.SqlClient; provider连接字符串=&"/><!-安装程序的根目录.-><目录ID ='TARGETDIR'名称='SourceDir'><!-安装到inetpub/wwwroot目录-><目录ID ="IISMain" Name ='inetpub'><目录ID ="WWWMain"名称='wwwroot'ComponentGuidGenerationSeed ='C38ED13E-E1E3-40DB-B1FA-39400C6B2BC4'><目录ID ='INSTALLLOCATION'Name =!(loc.ProductName)"><!-定义虚拟目录的组件.-->< Component Id ="WebVirtualDirComponent"Guid ="D814F88F-6E0C-4365-A411-2F9807522C3D"><!-WebVirtualDir:我们正在安装的虚拟目录.-><!-别名:Alias属性是我们将在IIS中看到的名称.<!-目录:目录"属性是其中的物理路径"属性IIS,需要绑定到上面指定的ID作为安装位置.-><!-WebSite:WebSite属性与< WebSite>中的元素设置文件(见下文).由于这是安装到默认网站",以便该元素不在组件之下.< iis:WebVirtualDir Id ="VDir" Alias ="[VIRTUALDIRECTORYVALUE]"Directory ="INSTALLLOCATION"WebSite ="DefaultWebSite"><!-将虚拟目录转换为Web应用程序.->< 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 =卸载"/></Component><!-组件-这决定了我们在安装中要包含的内容在这里,我们将更改web.config的Impersonation,将其调试为false和连接字符串.-><组件Id ="Web.config" Guid ="2ED81B77-F153-4003-9006-4770D789D4B6"><!-安装我们的web.config文件,这不是我们最初的MSBUILD的一部分-><文件Id ="Web.config"名称="Web.config"源="$(var.SolutionDir)!(loc.WebApplicationProjectName)\ Web.config" DiskId ="1" KeyPath ="yes"/><!-修改我们的web.config-在这里我们需要添加身份模拟,更改会话设置,添加连接字符串设置和设置调试设置-><!-确保身份设置存在->< util:XmlFile Id ="system.webidentity"File ="[INSTALLLOCATION] Web.config"Action ="createElement"ElementPath ="/configuration/system.web"名称=身份"SelectionLanguage ="XPath"Sequence ="1"/>< util:XmlFile Id ="system.webIdentityAttribute"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/identity"名称=假冒人"值=真"SelectionLanguage ="XPath"Sequence ="2"/>< util:XmlFile Id ="system.webIdentityAttribute2"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/identity"名称=密码"值="[IMPERSONATIONUSERPASSWORD]"SelectionLanguage ="XPath"Sequence ="3"/>< util:XmlFile Id ="system.webIdentityAttribute3"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/identity"名称="userName"值="[IMPERSONATIONUSER]"SelectionLanguage ="XPath"Sequence ="4"/>< util:XmlFile Id ="ModifyConnectionString"Action ="setValue"永久=是"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/connectionStrings/add [\ [] @ name ='!(loc.EntityName)'[\]]"名称="connectionString"值="[CONNECTION_STRING]数据源= [DBSERVER];初始目录= [DBNAME];集成安全性= 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"名称=模式"值="Windows"Sequence ="6"/><!-关闭调试->< util:XmlConfig Sequence ="7"Id ="SwitchOffDebug"File ="[INSTALLLOCATION] \ web.config"操作=创建"打开=安装"Node ="value"ElementPath ="/configuration/system.web/compilation"名称=调试"值="false"/><!-会话配置< sessionState mode ="InProc" timeout ="15"/>->< util:XmlFile Id ="system.websessionState"File ="[INSTALLLOCATION] Web.config"Action ="createElement"ElementPath ="/configuration/system.web"名称="sessionState"Sequence ="8"/>< util:XmlFile Id ="system.websessionStateAttribute"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/sessionState"名称=模式"值="InProc"Sequence ="9"/>< util:XmlFile Id ="system.websessionStateAttribute2"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/sessionState"名称=超时"值="15"Sequence ="10"/>< util:XmlFile Id ="system.websessionStateAttribute3"Action ="setValue"File ="[INSTALLLOCATION] Web.config"ElementPath ="/configuration/system.web/sessionState"名称="cookieName"值="[VIRTUALDIRECTORYVALUE]"Sequence ="11"/></Component>< iis:WebSite Id ='DefaultWebSite'Description =默认网站"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 =立即" Return ="check"/>< CustomAction Id ="GetIISAppPools" BinaryKey ="IisManager" DllEntry ="GetAppPools" Execute =立即" Return ="check"/>< InstallUISequence>< Custom Action ="GetIISWebSites" After ="CostFinalize" Overridable ="yes">未安装</Custom>< Custom Action ="GetIISAppPools" After ="CostFinalize" Overridable ="yes">未安装</Custom></InstallUISequence><功能ID ='ApplicationFeatures'Title =!(loc.ProductName)"级别='1'>< ComponentRef Id ='WebVirtualDirComponent'/>< ComponentGroupRef Id ="MyWebApp_Project"/>< ComponentRef Id ="Web.config"/></功能><!-指定UI->< Property Id ="WIXUI_INSTALLDIR"> INSTALLLOCATION</Property>< UIRef Id ="MyCustomUI"/> 

这是我的本地化文件:

 <?xml version ="1.0" encoding ="utf-8"?>< WixLocalization Culture ="zh-cn" xmlns ="http://schemas.microsoft.com/wix/2006/localization"><!-应用程序设置->< 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><!-数据库设置->< String Id ="EntityName"> MyEntities</String>< String Id ="ModelName"> MyModel</String></WixLocalization> 

I am currently attempting to modify my Wix(V3.5) installer to edit the Web.config settings of the .NET application i want to install. This is fine for normal ASP.NET applications but now im attempting to apply my Wix set up project to an Entity Framework .NET application , which as you may know has a more complicated Connection string setting with model .csdl and .ssdl settings.

So if my web.config connection string setting looks somehting like this :(where [DBSERVER] & [DBNAME] are properties retrived from a dialog )

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

And i edit my Web.config in my Product.Wsx file with somehting like this :

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

I get a connection string like this :

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

Which of course makes sense , since im asking it to replace the current connection string attribute with what i have defined in the value.

But what i really need here is to edit specific parts of my connection string and leave the remainder (is there some sort of replace action i can use here) ,i.e. leave all my model settings in place and just replace the database server and name etc as i need to. I used to do this with the Visual Studio installers no problem and it was so easy to use.

So my question is can this be done using util.XMLFile , or perhaps util:XmlConfig ? I have tried both without any luck.

Or is this not possible to do with util.XMLFile and will i have to do this in a CustomAction instead ? Any ideas would be of great help , thanks in advance ...

解决方案

I got this working in the end , in the end i didn't use custom actions for this particular setting , i use variables set up in my localisation file.

I did this because it would be the dev rather then the user who would know the model name and entities name (not a user via the install dialog , they wouldn't know this info), so i have a localisation file with different properties in it like product name etc , so i added in a model name and entites name to this. Everything else i get from the dialogs , entered by user : i.e. database name , virtual directory , impersonation user etc ...

If it helps anyone , here is what i came up with in the end for my web.config;This is the section of my product.wxs which deals with this issue. As you can see i have a connection string property at the top , with a placeholder for the loc.ModelName which is set in my localisation file:

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

Here is my localisation file:

<?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>

这篇关于使用Wix编辑Web.Config连接字符串设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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