Conditional Compilation - Visual Studio

Overview

One project I worked one was code inherited from a client. The client had certain web services available within their internal network but which were not accessible from outside their network. This presented a problem when I worked on their code — specifically the sections that called these web services. This article explains a work-around involving conditional compilation in Visual Studio.

Solution

(1) Create a new Build Configuration via Build menu > Configuration Manager > Active Solution Configuration = {New...} > (call it the name of your company).

(2) In the code where the web service is called, conditionally "remove" the web service calls, like the following example.

#If CONFIG <> "AcmeCompany" Then

    result = webServiceClass.WebMethod();

#End If

(3) Make the new build configuration the active one. This can be done via the dropdown on the Standard toolbar, or via Build menu > Configuration Manager > Active Solution Configuration dropdown.