app.config w zależności od konfiguracji – TransformXml
May
29
Tworzymy kopie App.config o nazwach App.<nazwa_konfiguracji>.config (App.Debug.config)
w pliku konfiguracyjnym projektu w sekcji zawierającej wpis z App.config dodajemy wpisy z pozostałymi wersjami plików konfiguracyjny według poniższego przykładu:
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="App.Debug.config">
<DependentUpon>App.config</DependentUpon>
</None>
<None Include="App.Release.config">
<DependentUpon>App.config</DependentUpon>
<SubType>Designer</SubType>
</None>
...
</ItemGroup>
pod ostatnim elementem ItemGroup ale przed zamknięciem tagu </Project> dodajemy wpis:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Następnie
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="App_config_AfterCompile" AfterTargets="AfterCompile" Condition="Exists('App.$(Configuration).config')">
<TransformXml Source="App.$(Configuration).config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.config" />
<ItemGroup>
<AppConfigWithTargetPath Remove="App.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<Target Name="App_config_AfterPublish" AfterTargets="AfterPublish" Condition="Exists('App.$(Configuration).config')">
<PropertyGroup>
<DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
</PropertyGroup>
<Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
</Target>