Labels

Wednesday, April 10, 2013

Use web.config transformations with other projects

Transformation over your ServiceReferences.ClientConfig files


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.

No comments:

Post a Comment