0

I use a web service in a C# project. I test the project both online and on localhost. I have code like:

 //var proxy = new PanoNestWebsite.ScheduleImagePanonest.ScheduleImageProcessingClient();
 //var s = new ScheduleImagePanonest.ScheduleImageProcessing();
 var proxy = new ScheduleImageLocal.ScheduleImageProcessingClient();
 var s = new ScheduleImageLocal.ScheduleImageProcessing();

For example, If I want to deploy it online I uncomment the first two lines, and comment the last two.

But I have much more changes to make, and I don't like commenting and uncommenting all lines. How can I use proprocessing directives to define a variable like: IsLocal and I just set it to true to include the local code, or to false to include the server code? How can I write something like this?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Ryan
  • 5,304
  • 25
  • 68
  • 120
  • You can use the [`#if` preprocessor directive](http://stackoverflow.com/questions/975355/ifdef-in-c-sharp); wrap your code in the appropriate block. – Cody Gray Jan 07 '12 at 16:13

2 Answers2

3

If this is the kind of thing that you're dealing with, I'd recommend an alternative approach, possibly some IOC/DI where both of these classes implement an interface, and you configure in your app.config/web.config which type to point to it.

So instead of

var proxy = new ScheduleImageLocal.ScheduleImageProcessingClient();

you'd instead do

var proxy = IoCFactory.Resolve<IScheduleImagingProcessingClient>();

or something to that effect, however your library works.

If you use Unity, you would point the IScheduleImagingProcessingClient interface to a concrete type in your config file.

Joe Enos
  • 38,150
  • 11
  • 77
  • 129
1

You can use the project properties to define values or #define SOME_KEY then:

#if KEY

    ... Code goes here

#endif
Mithrandir
  • 23,979
  • 6
  • 45
  • 64