How to define build configurations for different API base URLs in UWP Project VS2015?

1.3k views Asked by At

I have started building a UWP application. In it, I am going to have different API deployments which changes the BaseURL like following:

//DEV Api
public const string BaseURL = "http://mydevapi.com/devApi";
//Test Api
public const string BaseURL = "http://mytestapi.com/testApi";
//UAT Api
public const string BaseURL = "http://myuatapi.com/uatApi";
//Live Api
public const string BaseURL = "http://myliveapi.com/LiveApi";

So basically I want a way to change the baseURL on dynamically based on what kind of build I am making i.e. Dev, Test or Live.

I have previous experience of iOS and in XCode we create different schemes for this purpose.

I have seen the documentation and I think the best bet I have in c# and Visual studio is Build configuration as it is described as:

You can create several build configurations for a solution. For example, you can configure a debug build that your testers can use to find and fix problems, and you can configure different kinds of builds that you can distribute to different customers.

I have created a new build configuration named "AppName.Dev" but how can I use it to change the baseURL constant to Dev url?

This might come off as a basic question and shows my inexperience with c# and Visual studio but I can't figure out how do I make it materialize even after reading the documentation on it. Help a fella out?

EDIT: After reading this article, I have created four different configurations and defined conditional compilation symbols like following:

enter image description here

I have now used the defined compilation symbols in my constants file like this:

public const string BaseURL = "";
#if DEV
public const string BaseURL = "http://mydevapi.com/devApi";
#elif TEST
public const string BaseURL = "http://mytestapi.com/testApi"
#elif UAT
public const string BaseURL = "http://myuatapi.com/uatApi"
#elif LIVE
public const string BaseURL = "http://myliveapi.com/LiveApi";
#endif

The symbols however don't seem to be working as it returns empty string for baseURL even though I am running it in DEV configuration. Will update when I sort it out.

EDIT 2: Figured it out. I had two projects in my solution and I was defining configs and their symbols in one project while using them in the other. Posting an answer.

1

There are 1 answers

4
NSNoob On BEST ANSWER

To add the build configurations like this, follow the following steps:

  1. Select your project.
  2. Go to build->configuration manager
  3. From Active Solution configuration drop down, select New...
  4. Name your new configuration and select whether it copies settings from Debug, release, ad hoc, App store or none based on your needs. enter image description here
  5. Add settings such as whether this configuration is for build/deploy or both, the preferred platform etc. as per your needs.
  6. Repeat step 4 & 5 for as many build configurations as you want to define.

This concludes adding new build configuration. The next part is to define conditional compilation symbols. For that, follow the following steps:

  1. Collapse your project tree.
  2. Locate Properties dropdown.
  3. Right click it and click Open.
  4. A properties menu will open.
  5. Select Build from the side menu.
  6. From the top drop down labelled Configuration, select your defined build configuration.
  7. Properties of that configuration will open below. In the textfield labelled "Conditional Compilation symbols", define as many symbols as you want, separated by ;.

Consult the following screenshot if you have any confusions:

enter image description here

With this part done, you can use your defined symbols in your API constants file like:

#if DEV
public const string BaseURL = "http://mydevapi.com/devApi";
#elif TEST
public const string BaseURL = "http://mytestapi.com/testApi"
#elif UAT
public const string BaseURL = "http://myuatapi.com/uatApi"
#elif LIVE
public const string BaseURL = "http://myliveapi.com/LiveApi";
#endif

Here, DEV, TEST, UAT and LIVE are my defined configurations.