Search code examples
c#visual-studiobuildconfiguration

Using custom build configuration in visual studio


In my client application I sometimes connect to localhost:1242\SomeService.asmx and some other times it connects to someDomain:1242\SomeService.asmx. In other words there are times when I want to test locally and some other times remotely.

The default options that VS gives you are debug and release. I want to create custom ones in fact I have just created a new build configuration: enter image description here

Anyhow how can I know in code if I am using that configuration?

I will like to do something like:

if(Configuration.Type == ConfigTypes.Local)
    ConectionString = "localhost:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote1)
    ConectionString = "SomeDomain1:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote2)
    ConectionString = "SomeDifDomain:1242:\SomeService.asmx";

Also release mode tends to be more efficient? How will I specify those settings?


Solution

  • You could define conditional compilation symbols.
    Project properties → Build tab → Conditional compilation symbols.
    Define there different symbols for different configurations, for instance:

    • SRV_LOCAL in the "Local" configuration;
    • SRV_REMOTE1 in the "Remote1" configuration;
    • SRV_REMOTE2 in the "Remote2" configuration.

    Then in the code:

    #if SRV_LOCAL
        private const string SERVER = "localhost";
    #elseif SRV_REMOTE1
        private const string SERVER = "SomeDomain1";
    #elseif SRV_REMOTE2
        private const string SERVER = "SomeDifDomain";
    #endif