简体   繁体   中英

My app.config did not transform the value

In my windows service application, I want to debug it by my configuration. Originally I used slowcheetah, however it just did not work. So I wanted to manually to transform it. There is a method to do it.

In the project file, I have

  <ItemGroup>
     <Content Include="App.config" />
     <Content Include="App.Debug.config">
        <DependentUpon>App.config</DependentUpon>
     </Content>
     <Content Include="App.TestDebug.config">
        <DependentUpon>App.config</DependentUpon>
     </Content>
     <Content Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </Content>
    <None Include="packages.config">
    </None>
 </ItemGroup>

Also I have

 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
 <Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
   <!-- Generate transformed app config in the intermediate directory -->
   <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
   <!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
     <AppConfigWithTargetPath Remove="app.config" />
     <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
     <TargetPath>$(TargetFileName).config</TargetPath>
    </AppConfigWithTargetPath>
</ItemGroup>
</Target>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
   Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

In App.TestDebug.config, the value is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
 <appSettings>
  <add key="veserver" value="xx.xx.xx.221" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

In App.Debug.config, the value is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
    <add key="veserver" value="xx.xx.xx.221" />

In App.config, the value is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
  <add key="veserver" value="xx.xx.xx.222" />

Now I debug it with

App.TestDebug.config

In the C# code ConfigurationManager.AppSettings["veserver"] is still

xx.xx.xx.222 Which means that it didn't transform the value. It is supposed to be xx.xx.xx.221

Why?

Find a solution by myself.

<ItemGroup>
 <Content Include="App.config" />
 <None Include="App.Debug.config">
    <DependentUpon>App.config</DependentUpon>
 </None>
 <None Include="App.TestDebug.config">
    <DependentUpon>App.config</DependentUpon>
 </None>
 <None Include="App.Release.config">
    <DependentUpon>App.config</DependentUpon>
</None>
<None Include="packages.config">
</None>

Change

Content

to

None

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM