1

I have a challenge for you.

I have an ASP.NET Web Application built in C# with Microsoft Visual Studio 2017. There is a line of code that we have had to include in order for it to run on a localhost when debugging by attaching it to a running process in a web browser.

Here is the issue. That line of code is not necessary when the process is running on the server.

I can remove that line when I submit the code in TFS. But it would be great if we could make the code somehow ignore that line depending on what platform it is running on.

How can you think this could be done?

xarzu
  • 8,657
  • 40
  • 108
  • 160

2 Answers2

1

I don't know a lot about ASP.NET, but can you make a build option named LOCALHOST or something similar that you set to true if you want it ran or not.

#if LOCALHOST
//do this line
#endif
Noble
  • 104
  • 10
  • This is a good idea. But if you make this build option in the project file, I assume you would have to remember not to check in the project file. – xarzu Apr 05 '19 at 17:16
  • It is a good idea but I checked bluiska as the answer. Albeit, one could also say yours is the answer too if the code on the server was also running in debug mode. – xarzu Apr 05 '19 at 18:16
  • Not necessarily, I'm assuming you have to rebuild for a debug/release version either way. Simply flipping the var to false when running the released version would work. But then you'd have to remember to set it for when you do need it. I think @bluiska answer would work better in this situation. – Noble Apr 05 '19 at 18:44
0

I've done this in C++ in the past using the #if directive. A quick google and this is also available in C#.

By defining #if CONDITION followed by a #endif, the code wrapped inside is only ran when compiled for the condition defined. In your case, you could have #if DEBUG and the code following would only execute in debug mode.

Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

bluiska
  • 369
  • 2
  • 12
  • #if DEBUG is the way to go. Running it locally and stepping through the code is obviously in debug mode and on the server it is not. – xarzu Apr 05 '19 at 18:15