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:
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.
To add the build configurations like this, follow the following steps:
This concludes adding new build configuration. The next part is to define conditional compilation symbols. For that, follow the following steps:
;
.Consult the following screenshot if you have any confusions:
With this part done, you can use your defined symbols in your API constants file like:
Here, DEV, TEST, UAT and LIVE are my defined configurations.