Подтвердить что ты не робот

Как использовать новые конфигурации конфигурации VS 2010 и применять их к другим файлам .config?

У меня есть настройки конфигурации в моем web.config для моего connectionStrings и т.д. Но я выделил некоторые области моего web.config в отдельные файлы, например) appSettings.config.

Как настроить Visual Studio и MSBuild для выполнения конфигурационных преобразований в этих дополнительных файлах конфигурации?

Я уже придерживался подхода web.config, чтобы связать файлы вместе в файле проекта веб-приложения, но преобразования не применяются автоматически.

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

Ответ 1

По умолчанию целевое управление преобразованием (TransformWebConfig) работает только в файле web.config.


Чтобы заставить его работать с вашим файлом appSettings.config, вам нужно:

  • Установите Build Action вашего файла на Content
  • Вызовите цель MSBuild TransformWebConfig с помощью ProjectConfigFileName=appSettings.config и Configuration=$(Configuration).

Чтобы вызвать MSBuild TransformWebConfig target для appSettings.config сразу после преобразования файлов web.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>

Ответ 2

Что-то, что делает это намного проще, взгляните на надстройку SlowCheetah VS... visualstudiogallery

Ответ 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>