我有一个大型的Silverlight项目,其中包含一个笨拙的web.config,它使用了对web.debug.config,web.uat.config和web.release.config文件的转换。
我没有将EntLib配置分离到EntLib.config中,并且匹配了EntLib.debug.config,EntLib.uat.config和EntLib.release.config文件。我编辑了.csproj文件并使用了DependentUpon,以便将文件嵌套在EntLib.config下。现在,当我使用Publish ...菜单选项将文件直接发布到测试服务器时,我正试图让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);
 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>
答案 0 :(得分:1)
我使用T4和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" #>
答案 1 :(得分:0)
我使用这篇文章解决了这个问题:Vishal Joshi撰写了Xml Document Transforms (XDT) for any XML file in your project并在此处发布了详细信息:How to apply transforms to EntLib.config。
我自己的解决方案遵循Vishal的选项,将他的XDT目标文件存储在项目中,以便它存储在源代码控制中,并且可供所有人使用,而不是将其本地存储在计算机上。