Brennan Stehling blogged back in 2007 about how to make use of the MSbuild tasks in Web Deployment Projects to merge web.config files directly in an MSBuild script without having to create a separate WDP project.
I'd like to use this technique in a non-web project (in this case a Windows Service exe app).
I'm currently trying this directly in the .csproj adding the following to the bottom of the file:
<UsingTask
TaskName="ReplaceConfigSections"
AssemblyFile="$(MSBuildExtensionsPath)\\Microsoft\\WebDeployment\\v9.0\\Microsoft.WebDeployment.Tasks.dll"/>
<PropertyGroup>
<WDTargetDir>$(MSBuildProjectDirectory)\\$(TargetFileName).config</WDTargetDir>
<ValidateWebConfigReplacement>false</ValidateWebConfigReplacement>
<UseExernalWebConfigReplacementFile>true</UseExernalWebConfigReplacementFile>
</PropertyGroup>
<ItemGroup Condition=" '$(Configuration)' == '' ">
<WebConfigReplacementFiles Include="$(SolutionDir)MyApp.prod.config">
<Section>appSettings</Section>
</WebConfigReplacementFiles>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Release' ">
<WebConfigReplacementFiles Include="$(SolutionDir)MyApp.prod.config">
<Section>appSettings</Section>
</WebConfigReplacementFiles>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<WebConfigReplacementFiles Include="$(SolutionDir)MyApp.dev.config">
<Section>appSettings</Section>
</WebConfigReplacementFiles>
</ItemGroup>
<Target Name="Build">
<ReplaceConfigSections
RootPath="$(WDTargetDir)"
WebConfigReplacementFiles="@(WebConfigReplacementFiles)"
UseExternalConfigSource="$(UseExernalWebConfigReplacementFile)"
ValidateSectionElements="$(ValidateWebConfigReplacement)"
/>
</Target>
Sadly, it doesn't work.
I'm guessing this is because it expects the parameter WDTargetDir
to be a folder path and is hard-coded to assume it should process a file named web.config
, and obviously I'm trying to hack it to edit MyApp.exe.config
I simply receive this error in the build output:
error : web.config(1): error WDP00002: missing section appSettings. Update of web.config Failed