2010-05-11 21 views
10

我已經在我的web.config中爲connectionStrings等設置了一些配置轉換。但是我已經將web.config的某些區域分隔成了單獨的文件,例如appSettings.config。如何使用新的VS 2010配置轉換並將它們應用到其他.config文件?

如何配置Visual Studio和MSBuild在這些額外的配置文件上執行配置轉換?

我已經按照web.config的方法將文件關聯到我的web應用程序項目文件中,但轉換不會自動應用。

<ItemGroup> 
    <Content Include="appSettings.Debug.config"> 
     <DependentUpon>appSettings.config</DependentUpon> 
    </Content> 
</ItemGroup> 

回答

11

默認情況下,目標管理轉變(TransformWebConfig)只在web.config文件的工作。


爲了讓您appSettings.config文件工作,你就必須:

  • 設置你的文件的Build ActionContent
  • 調用的MSBuild目標TransformWebConfigProjectConfigFileName=appSettings.configConfiguration=$(Configuration)

要只是web.config文件轉換後調用的MSBuild TransformWebConfig目標appSettings.config,你需要在你的項目文件的最後補充一點:

<PropertyGroup> 
    <!-- Name of your custom config file --> 
    <ConfigFileName>appSettings.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> 
+1

這似乎確實起作用。我的appSettings.config文件已轉換,但在部署期間,最終的配置文件未放入輸出文件夾。 – 2010-05-11 17:02:22

+0

我更新了代碼,使用BeforeTarget和AfterTarget,而不是重寫TransformWebConfig dependson目標,並處理自動參數化步驟。現在你自定義的配置文件應該在輸出文件夾與web.config – 2010-05-12 09:00:50

+1

我也試過你的解決方案。像@Wallace Breza一樣,它看起來很有效:我可以在obj \ $(Configuration)\ TransformWebConfig \轉換中看到已轉換的EntLib.config文件,但未部署。我嘗試過使用(右鍵單擊項目)> Publish ...(發佈方法:文件系統)方法,並使用Project> Build Deployment Package,然後在另一臺機器上運行。兩者都以原始形式將EntLib.config和EntLib。($ Configuration).config文件放在一起。 – SGarratt 2011-03-12 14:37:08

5

這是令這個有很多更容易,看看SlowCheetah VS加載項在... visualstudiogallery

+0

謝謝Steve!我絕對同意你的看法。很容易。我在尋找你確切的建議。謝謝 :) – curiousBoy 2013-11-22 17:16:23

3

這是代碼適用於我:

<PropertyGroup> 
    <!-- Name of your custom config file --> 
    <ConfigFileName>ConnectionStrings.config</ConfigFileName> 
    <ConfigTransformFileName>ConnectionStrings.$(Configuration).config</ConfigTransformFileName> 
    </PropertyGroup> 
    <PropertyGroup> 
    <!-- 
     This property is used to handle circular dependency between 
     TransformWebConfig and our custom target TransformAppConfig 
    --> 
    <FirstRun Condition="$(FirstRun) == ''">true</FirstRun> 
    </PropertyGroup> 
    <Target Name="AddConfigToTransform" AfterTargets="CollectWebConfigsToTransform"> 
    <ItemGroup> 
     <WebConfigsToTransform Include="@(FilesForPackagingFromProject)" Condition="'%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)'"> 
     <TransformFile>%(RelativeDir)$(ConfigTransformFileName)</TransformFile> 
     <TransformOriginalFile>$(TransformWebConfigIntermediateLocation)\original\%(DestinationRelativePath)</TransformOriginalFile> 
     <TransformOutputFile>$(TransformWebConfigIntermediateLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile> 
     <TransformScope>$([System.IO.Path]::GetFullPath($(_PackageTempDir)\%(DestinationRelativePath)))</TransformScope> 
     </WebConfigsToTransform> 
    </ItemGroup> 
    </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> 
相關問題