ConfigSwitcher: ServiceReferences.ClientConfig Switcher Utility

Silverlight 2 uses ServiceReferences.ClientConfig to store WCF related configuration. It is packaged and deployed along with the application in XAP file. Since XAP is a compressed file container (similar to zip), it is possible to uncompress the XAP file, change the desired configuration setting, and compress the results into a new XAP file for deployment. However if you have multiple sites(dev, qa, staging, prod/release, training) that you need to deploy to, this process can become very cumbersome and error prone.

In order to automate the creation of XAP file for the appropriate target environment, I wrote a small console utility that switches proper configuration file based on the selected solution configuration. You provide different configuration files for each target site, appended with configuration name, like ServiceReferences.ClientConfig.qa, ServiceReferences.ClientConfig.Release and so on.

image

Then, you can create new Configurations using Configuration Manager:

 image

Next you setup the switcher utility in pre and post build

image

Select the desired build configuration

image

Now when you build the Silverlight application, configuration switcher will switch in proper configuration file. It does this by renaming file in pre build stage and renaming it back in post build. For example if you are building a release build, following happens during pre and post build.

Pre build

ServiceReferences.ClientConfig –> rename –> ServiceReferences.ClientConfig.build

ServiceReferences.ClientConfig.Release–> rename –> ServiceReferences.ClientConfig

Post Build

ServiceReferences.ClientConfig –> rename –> ServiceReferences.ClientConfig.Release

ServiceReferences.ClientConfig.build –> rename –> ServiceReferences.ClientConfig

Here is the code for the program:

namespace ConfigSwitcherApp {
    class Program {
        static void Main(string[] args) {
            //
            if (0 == args.Length || args[0] == "Debug" || args[0] == "DEBUG") {
                return;
            }
            string configurationName = args[0];
            string preBuild = args[1];
            string projectDir = args[2];
            //
            if ("True" == preBuild) {
                RunPreBuild(configurationName, projectDir);
            }
            else if ("False" == preBuild){
                RunPostBuild(configurationName, projectDir);
            }

        }

        private static void RunPreBuild(string configurationName, string projectDir) {
            string path = projectDir + "\\ServiceReferences.ClientConfig";
            string newPath = projectDir + "\\ServiceReferences.ClientConfig.build";
            File.Move(path, newPath);
            //
            if (!string.IsNullOrEmpty(configurationName)) {
                path = projectDir + "\\ServiceReferences.ClientConfig." + configurationName;
                newPath = projectDir + "\\ServiceReferences.ClientConfig";
                File.Move(path, newPath);
            } 

        }

        private static void RunPostBuild(string configurationName, string projectDir) {
            string path = null;
            string newPath = null;
            if (!string.IsNullOrEmpty(configurationName)) {
                path = projectDir + "\\ServiceReferences.ClientConfig";
                newPath = projectDir + "\\ServiceReferences.ClientConfig." + configurationName;
                File.Move(path, newPath);
            }
            path = projectDir + "\\ServiceReferences.ClientConfig.build";
            newPath = projectDir + "\\ServiceReferences.ClientConfig";
            File.Move(path, newPath);
            //
        }
    }
}

Usage:

For Pre build

C:\ConfigSwitcherApp.exe $(ConfigurationName) True $(ProjectDir)

For Post build

C:\ConfigSwitcherApp.exe $(ConfigurationName) False $(ProjectDir)

Console Application takes three parameters (all required)

Parameter 1 : Configuration Name for file suffix, use Visual Studio macro - $(ConfigurationName)

Parameter 2 : True for pre build, to switch in target config file and False for post build to switch out target config file (and switch in design time config file)

Parameter 3 : Project directory, use Visual Studio macro - $(ProjectDir)

Source Code: ConfigSwitcherApp.zip

Hopefully this helps you to automate you build process and eliminate errors when deploying to multiple sites.

Technorati Tags:
Published Tuesday, November 04, 2008 11:27 AM by manish.dalal
Filed under: , ,

Comments

# 2008 November 05 - Links for today « My (almost) Daily Links

Wednesday, November 05, 2008 4:24 AM by 2008 November 05 - Links for today « My (almost) Daily Links

Pingback from  2008 November 05 - Links for today « My (almost) Daily Links

# Silverlight News for November 05, 2008

Wednesday, November 05, 2008 4:32 AM by Silverlight News for November 05, 2008

Pingback from  Silverlight News for November 05, 2008

# Silverlight ServiceReferences.ClientConfig Alternatives

Monday, February 23, 2009 3:36 PM by Manish Dalal's blog

Silverlight uses the ServiceReferences.ClientConfig file to store and lookup WCF related configuration

# Silverlight Travel » Silverlight ServiceReferences.ClientConfig Alternatives

Pingback from  Silverlight Travel » Silverlight ServiceReferences.ClientConfig Alternatives

# woodbury commons ny: Silverlight ServiceReferences.ClientConfig Alternatives « 68686886

Pingback from  woodbury commons ny: Silverlight ServiceReferences.ClientConfig Alternatives «  68686886

Leave a Comment

(required) 
(required) 
(optional)
(required)