Using XDT with Worker Role App.configs
http://blog.abstractlabs.net/2011_02_01_archive.html
Recently I wrote a worker role in windows azure and was hoping the app.config would transform much like the web.config in the web roles. Sadly this is not the case. The configuration transforms are very useful when making a distinction between production, staging and development environments. After a little research, I did discover how to roll my own hopefully Microsoft will extend this feature to the rest of the azure project types.
Like the web roles this transform is only executed when a deploy is executed, here is how it was done:
- Un-load the project containing your worker role and app.config
- Edit the project file at the bottom add the following lines:1234567
<
usingtask
taskname
=
"TransformXml"
assemblyfile
=
"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"
>
<
target
name
=
"AfterCompile"
condition
=
"exists('app.$(Configuration).config')"
>
<
transformxml
source
=
"app.config"
destination
=
"$(IntermediateOutputPath)$(TargetFileName).config"
transform
=
"app.$(Configuration).config"
>
</
transformxml
></
target
>
</
usingtask
>
- Add the following Item Group:1234567891011
<
ItemGroup
>
<
Content
Include
=
"App.config"
/>
<
Content
Include
=
"App.Debug.Config"
>
<
DependentUpon
>App.Config</
DependentUpon
>
<
SubType
>Designer</
SubType
>
</
Content
>
<
Content
Include
=
"App.Release.Config"
>
<
DependentUpon
>App.Config</
DependentUpon
>
</
Content
>
</
ItemGroup
>
- Now add the new configuration files to the project App.Debug.config, App.Release.config
- Reload the worker role project project
- Unload the cloud service project that references the worker role
- Open the file for edit and add the following line:12345
<
Target
Name
=
"CopyWorkerRoleConfigurations"
BeforeTargets
=
"AfterPackageComputeService"
>
<
Copy
SourceFiles="..\{<b>PROJECT PATH</
b
>}\obj\$(Configuration)\{<
b
>ASSEMBLY NAME</
b
>}.dll.config" DestinationFolder="$(IntermediateOutputPath)\{<
b
>PROJECT NAME</
b
>}" OverwriteReadOnlyFiles="true"/>
</
Target
>
- Reload the project file