modify silverlight ServiceReferencesClientConfig after a build

I use NANT to automate the build process, now I face a little problem with silverlight. When you reference a service the designer of visual studio generate a ServiceReferences.ClientConfig  with configuration of the service, it contains the address of the service and it gets compiled inside the.xap file generated by compilation

I use to point to a localhost machine during debugging, but in the real environment the service maybe is in another machine, so I need to find a way to point the client to the right service inside a nant build file. Since.xap files are nothing but a zip file I try to setup an action to unzip file, modify the ServiceReferences.ClientConfig, zip the file again and the game is done….exept that this does not works. It seems that Nant task <unzip> generates error when It tries to uncompress the xap file.

The solution was simple, I included 7z.exe (7-zip) on the buildTools directory, and simply instruct nant to uncompress with 7z.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<target name="UpdateSilverlightConfiguration" depends="GetPublishingProperties" >

    <mkdir dir="${DebugSitePath}\ClientBin\Temp" />

    <exec program="${sevenzip}" 
            commandline=" x ${DebugSitePath}\ClientBin\RepManagement.SIlverlight.xap -o${DebugSitePath}\ClientBin\Temp -y" />

    <copy
      file="${ConfigurationDir}\ServiceReferences.ClientConfig"
      tofile="${DebugSitePath}\ClientBin\Temp\ServiceReferences.ClientConfig"
      overwrite="True" />

    <zip zipfile="${DebugSitePath}\ClientBin\Temp\RepManagement.SIlverlight.xap"
            ziplevel="9" failonerror="true" >
        <fileset basedir="${DebugSitePath}\ClientBin\Temp" >
            <include name="**"/>
        </fileset>
    </zip>

    <copy
      file="${DebugSitePath}\ClientBin\Temp\RepManagement.SIlverlight.xap"
      tofile="${DebugSitePath}\ClientBin\RepManagement.SIlverlight.xap"
      overwrite="True" />

    <delete dir="${DebugSitePath}\ClientBin\Temp\" />
</target>

This is a very simple approach. I set 7z.exe in the build folder so it gets downloaded when subversion update the project directory, then I set a variable called sevenzip that points to the executable. Then uncompress the xap file, copy the production configuration from the configuration folder, overwriting the old one, then zipping all again.

This operation has a double advantage, because I can zip the xap file with higher compression rate to make it smaller.

alk.

Tags: Silverlight