如何将配置转换应用于 EntLib.config?

发布于 2024-10-25 10:43:29 字数 2904 浏览 3 评论 0原文

我有一个大型 Silverlight 项目,其中有一个笨重的 web.config,它使用了对 web.debug.config、web.uat.config 和 web.release.config 文件的转换。

我没有将我的 EntLib 配置与匹配的 EntLib.debug.config、EntLib.uat.config 和 EntLib.release.config 文件分离到 EntLib.config 中。我编辑了 .csproj 文件并使用 DependentUpon,以便这些文件嵌套在 EntLib.config 下。现在,当我使用“发布...”菜单选项将文件直接发布到测试服务器时,我尝试让 VS2010 应用转换。

我一直在尝试应用 this 如下所示,但它似乎不起作用。我可以在 obj\$(Configuration)\TransformWebConfig\transformed 中看到转换后的 EntLib.config 文件,但它尚未部署。我也尝试过使用 Project >构建部署包,然后我在另一台机器上运行它。两者都给我留下了原始形式的 EntLib.config 以及旁边的每个 EntLib.($Configuration).config 文件。应该有效吗?任何人可以提供的任何帮助将不胜感激。

<PropertyGroup>
    <ConfigFileName>EntLib.config</ConfigFileName>
  </PropertyGroup>
  <PropertyGroup>
    <!-- This property is used to handle circular dependency between
    TransformWebConfig and our custom target TransformAppConfig   -->
    <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
  </PropertyGroup>
  <!-- This target will be called one time after a call to TransformWebConfig -->
  <Target Name="TransformAppConfig" AfterTargets="TransformWebConfig" Condition="$(FirstRun) == 'true'">
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="TransformWebConfig" Properties="ProjectConfigFileName=$(ConfigFileName);&#xD;&#xA;                         Configuration=$(Configuration);                        &#xD;&#xA;                         FirstRun=false" />
  </Target>
  <!-- This target will be called one time before PreAutoParameterizationWebConfigConnectionStrings
       to add $(ConfigFileName) to autoparameterization step -->
  <Target Name="AddToAutoParameterizationStep" BeforeTargets="PreAutoParameterizationWebConfigConnectionStrings">
    <ItemGroup>
      <_WebConfigsToAutoParmeterizeCS Include="@(FilesForPackagingFromProject)" Condition="('%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)') And !%(FilesForPackagingFromProject.Exclude)">
        <TransformOriginalFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
      </_WebConfigsToAutoParmeterizeCS>
      <_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
      </_WebConfigsToAutoParmeterizeCSOuputFiles>
    </ItemGroup>
  </Target>

I had a large Silverlight project with an unwieldy web.config, which used transforms against the web.debug.config, web.uat.config, and web.release.config files.

I've not separated out my EntLib configuration into EntLib.config, with matching EntLib.debug.config, EntLib.uat.config and EntLib.release.config files. I've edited the .csproj file and used DependentUpon so that the files are nested under EntLib.config. Now I'm trying to get VS2010 to apply the transforms when I use the Publish... menu option to publish the files straight to the test server.

I've been trying to apply this as shown below but it doesn't seem to work. I can see the transformed EntLib.config file in obj\$(Configuration)\TransformWebConfig\transformed but it isn't deployed. I've also tried using Project > Build Deployment Package which I've then run on another machine. Both leave me with EntLib.config in its original form plus each of the EntLib.($Configuration).config files alongside it. Should it work? Any help anyone can offer would be appreciated.

<PropertyGroup>
    <ConfigFileName>EntLib.config</ConfigFileName>
  </PropertyGroup>
  <PropertyGroup>
    <!-- This property is used to handle circular dependency between
    TransformWebConfig and our custom target TransformAppConfig   -->
    <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
  </PropertyGroup>
  <!-- This target will be called one time after a call to TransformWebConfig -->
  <Target Name="TransformAppConfig" AfterTargets="TransformWebConfig" Condition="$(FirstRun) == 'true'">
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="TransformWebConfig" Properties="ProjectConfigFileName=$(ConfigFileName);
                         Configuration=$(Configuration);                        
                         FirstRun=false" />
  </Target>
  <!-- This target will be called one time before PreAutoParameterizationWebConfigConnectionStrings
       to add $(ConfigFileName) to autoparameterization step -->
  <Target Name="AddToAutoParameterizationStep" BeforeTargets="PreAutoParameterizationWebConfigConnectionStrings">
    <ItemGroup>
      <_WebConfigsToAutoParmeterizeCS Include="@(FilesForPackagingFromProject)" Condition="('%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)') And !%(FilesForPackagingFromProject.Exclude)">
        <TransformOriginalFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
      </_WebConfigsToAutoParmeterizeCS>
      <_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
      </_WebConfigsToAutoParmeterizeCSOuputFiles>
    </ItemGroup>
  </Target>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-11-01 10:43:29

我使用 T4T4 。 microsoft.com/en-us/library/bb126245.aspx" rel="nofollow">TextTransform.exe 根据构建配置创建不同的配置。您可以查看我的 app.config 片段,但相同的技术也适用于 web.config。

1) 项目结构

ProjectDir
  App_Config 
    Configuration.tt     // template for all configs
    Debug.App.tt         // settings for Debug
    Release.App.tt       // settings for Release
    ProductDeploy.App.tt // settings for deploy
  App.config             // autogenerated. Ignored in SVN
  project.csproj          

2) project.csproj 修改允许为指定平台/配置拥有最新的配置。

  <PropertyGroup>
    <T4Template>$(ProjectDir)\App_Config\$(Configuration).App.tt</T4Template>
    <T4CommonTemplate>$(ProjectDir)\App_Config\Configuration.tt</T4CommonTemplate>
    <T4Config>$(ProjectDir)\App.config</T4Config>
    <T4LastConfiguration>$(BaseIntermediateOutputPath)\$(Configuration).t4lastbuild</T4LastConfiguration>
  </PropertyGroup>
  <Target Name="BeforeBuild" DependsOnTargets="ExecuteT4Templates" />
  <Target Name="ExecuteT4Templates"     Inputs="$(T4Template);$(T4CommonTemplate);$(T4LastConfiguration)" Outputs="$(T4Config)">
    <MakeDir Directories="$(BaseIntermediateOutputPath)" Condition="!Exists('$(BaseIntermediateOutputPath)')" />
    <ItemGroup>
      <T4ConfigFlags Include="$(BaseIntermediateOutputPath)\*.t4lastbuild" />
    </ItemGroup>
    <Delete Files="@(T4ConfigFlags)" />
    <WriteLinesToFile File="$(T4LastConfiguration)" Lines="T4 Succeeded" Overwrite="true" />
    <Exec Command="TextTransform "$(T4Template)" -out "$(T4Config)"" WorkingDirectory="C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2\" />
  </Target>
  <Target Name="AfterClean">
    <ItemGroup>
      <T4ConfigFlags Include="$(BaseIntermediateOutputPath)\*.t4lastbuild" />
    </ItemGroup>
    <Delete Files="@(T4ConfigFlags)" />
  </Target>

3) Configuration.tt 示例

<#@ template language="C#"#>
<#@ output extension= ".config"#>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
        <add name = "NameSpace.Properties.Settings.SomeConnectionString"
         connectionString = "<#= this.SomeConnectionString #>"
         providerName     = "System.Data.SqlClient" />    
  </connectionStrings>

  <applicationSettings>
    <NameSpace.Properties.Settings>
            <setting name="DefAppSetting" serializeAs="String">
                <value><#= this.DefAppSetting #></value>
            </setting>          
        </NameSpace.Properties.Settings>    
  </applicationSettings>
</configuration>

<#+
  string SomeConnectionString = "default SomeConnectionString";
  string DefAppSetting = "some_value";
#>

4) Debug.App.tt 示例

<#
  SomeConnectionString = "Data Source=.;Initial Catalog=SomeDB;Integrated Security=True";  
  DefAppSetting = "debug_some_value";  
#>
<#@ include file="Configuration.tt" #>

I use T4 and TextTransform.exe to create different configs based on build configuration. You can take a look on my snippets for app.config, but the same technique can be applied for web.config.

1) Project structure

ProjectDir
  App_Config 
    Configuration.tt     // template for all configs
    Debug.App.tt         // settings for Debug
    Release.App.tt       // settings for Release
    ProductDeploy.App.tt // settings for deploy
  App.config             // autogenerated. Ignored in SVN
  project.csproj          

2) project.csproj modification allows to have up-to-date config for specified Platform/Configuration.

  <PropertyGroup>
    <T4Template>$(ProjectDir)\App_Config\$(Configuration).App.tt</T4Template>
    <T4CommonTemplate>$(ProjectDir)\App_Config\Configuration.tt</T4CommonTemplate>
    <T4Config>$(ProjectDir)\App.config</T4Config>
    <T4LastConfiguration>$(BaseIntermediateOutputPath)\$(Configuration).t4lastbuild</T4LastConfiguration>
  </PropertyGroup>
  <Target Name="BeforeBuild" DependsOnTargets="ExecuteT4Templates" />
  <Target Name="ExecuteT4Templates"     Inputs="$(T4Template);$(T4CommonTemplate);$(T4LastConfiguration)" Outputs="$(T4Config)">
    <MakeDir Directories="$(BaseIntermediateOutputPath)" Condition="!Exists('$(BaseIntermediateOutputPath)')" />
    <ItemGroup>
      <T4ConfigFlags Include="$(BaseIntermediateOutputPath)\*.t4lastbuild" />
    </ItemGroup>
    <Delete Files="@(T4ConfigFlags)" />
    <WriteLinesToFile File="$(T4LastConfiguration)" Lines="T4 Succeeded" Overwrite="true" />
    <Exec Command="TextTransform "$(T4Template)" -out "$(T4Config)"" WorkingDirectory="C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2\" />
  </Target>
  <Target Name="AfterClean">
    <ItemGroup>
      <T4ConfigFlags Include="$(BaseIntermediateOutputPath)\*.t4lastbuild" />
    </ItemGroup>
    <Delete Files="@(T4ConfigFlags)" />
  </Target>

3) Configuration.tt sample

<#@ template language="C#"#>
<#@ output extension= ".config"#>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
        <add name = "NameSpace.Properties.Settings.SomeConnectionString"
         connectionString = "<#= this.SomeConnectionString #>"
         providerName     = "System.Data.SqlClient" />    
  </connectionStrings>

  <applicationSettings>
    <NameSpace.Properties.Settings>
            <setting name="DefAppSetting" serializeAs="String">
                <value><#= this.DefAppSetting #></value>
            </setting>          
        </NameSpace.Properties.Settings>    
  </applicationSettings>
</configuration>

<#+
  string SomeConnectionString = "default SomeConnectionString";
  string DefAppSetting = "some_value";
#>

4) Debug.App.tt sample

<#
  SomeConnectionString = "Data Source=.;Initial Catalog=SomeDB;Integrated Security=True";  
  DefAppSetting = "debug_some_value";  
#>
<#@ include file="Configuration.tt" #>
眼趣 2024-11-01 10:43:29

我使用这篇文章解决了这个问题: 针对项目中任何 XML 文件的 Xml 文档转换 (XDT),作者:Vishal Joshi,并在此处发布了详细信息:如何将转换应用于 EntLib.config

我自己的解决方案遵循 Vishal 的选择,将其 XDT 目标文件存储在项目中,以便将其存储在源代码管理中并可供每个人使用,而不是将其存储在本地计算机上。

I solved this using this article: Xml Document Transforms (XDT) for any XML file in your project by Vishal Joshi and posted the specifics here: How to apply transforms to EntLib.config.

My own solution followed Vishal's option to store his XDT targets file in the project so that it gets stored in source control and is available to everyone, rather than storing it locally on the machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文