加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

.net – 使用Wix编辑Web.Config连接字符串设置

发布时间:2020-12-25 11:09:46 所属栏目:资源 来源:网络整理
导读:我目前正在尝试修改我的Wix(V3.5)安装程序,以编辑我想要安装的.NET应用程序的Web.config设置.这对于普通的ASP.NET应用程序来说很好,但现在我试图将我的Wix设置项目应用到Entity Framework .NET应用程序,您可能知道它有一个更复杂的连接字符串设置与模型.csdl
副标题[/!--empirenews.page--]

我目前正在尝试修改我的Wix(V3.5)安装程序,以编辑我想要安装的.NET应用程序的Web.config设置.这对于普通的ASP.NET应用程序来说很好,但现在我试图将我的Wix设置项目应用到Entity Framework .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中执行此操作?
任何想法都会有很大的帮助,在此先感谢…

解决方法

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

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

如果它对任何人有帮助,这就是我最终为我的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"/>

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读