I’ve blogged a before for web.config transformations and how I would like to see them in other projects. While for Windows Azure cloud service project there is already similar feature implemented, I wonder why there is no out of the box support for other project types.
Here I will reveal the powerful, yet simple implementation of such transformation over your ServiceReferences.ClientConfig files. Thus you will no longer wonder what are your service endpoints, and which endpoint you are using. And this is not Windows Azure specific. It is relevant for any Silverlight project.
Just implement these simple steps:
1. Edit your .csproj file of the Silverlight application. Add the following block:
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="BeforeBuild"
Condition="exists('ServiceReferences.$(Configuration).ClientConfig')">
<!-- Generate transformed app config in the intermediate directory –>
<TransformXml Source="ServiceReferences.ClientConfig"
Destination="$(TargetDir)\ServiceReferences.ClientConfig"
Transform="ServiceReferences.$(Configuration).ClientConfig" />
<!-- Force build process to use the transformed configuration file from now on. –>
<ItemGroup>
<Content Remove="ServiceReferences.ClientConfig"/>
<ContentWithTargetPath
Include="$(TargetDir)\ServiceReferences.ClientConfig">
<TargetPath>ServiceReferences.ClientConfig</TargetPath>
</ContentWithTargetPath>
</ItemGroup>
</Target>
right after:
<Import
Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\$(SilverlightVersion)\Microsoft.Silverlight.CSharp.targets" />
2. Add new XML file to your project. Name it ServiceReferences.[BuildConfiguration].ClientConfig. Where [BuildConfiguration] can be the name of *any* build configurations you have defined for your project. The default build configurations are “Debug” and “Release”, but you may add as many as you like, to suit your development/testing/staging/live environments. Remember to set “Build Action” to “None”, and “Copy to output directory” to “Never”:

3. Add the required content in that custom file. For example, if you want to just change an endpoint for a service, you will have something like this (ServiceReferences.Debug.ClientConfig):
<?xml version="1.0" encoding="utf-8"?>
<configuration
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.serviceModel>
<client>
<endpoint
address="http://127.0.0.1:81/DummyService.svc"
xdt:Transform="SetAttributes" />
</client>
</system.serviceModel>
</configuration>
For more information on XML transformations supported, please take a look at the MSDN documentation for Web.Config transformations. Do not panic! The documentation is for “web.config” transformations, but these are just XML transformations that can transform any XML file ![]()
1 comments:
This is exactly what I'm looking for. Thanks for sharing!
Post a Comment