简体   繁体   中英

Visual Studio 2012 - Conditional Using (service reference)

In my code I currently have:

#if (DEBUG)
using GetNewRecordNumber = {dev web service reference}.GetNewRecordNumber;
#else
using GetNewRecordNumber = {prod web service reference}.GetNewRecordNumber;
#endif 

So that I can do:

 using (GetNewRecordNumber.Tickets_GetNewRecordNumber getNum = new GetNewRecordNumber.Tickets_GetNewRecordNumber())

in my code. This all works fine, but I would like to make the switch in the Application Settings so that I can change it at run time. Is there a way to pull this off so the switch can happen on run time other then duplicating code?

Thanks!

The preprocessor directive works jut before the compilation, so it won't work at rune time. Sorry, but you must pull off the "#" directives and use the ConfigurationManager.AppSettings instead.

Not sure if you would consider this duplicating code, but perhaps the most straightforward approach would be to make a wrapper class for your services and use that wrapper class in places like the above. You're already duplicating tons of code with these #if blocks, so using a wrapper might even reduce your total code and make it cleaner.

Another option would be to manually modify your service references so that they inherit from a common interface that defines all the members you need, but that may not be a great idea if you need to update the references often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM