How to find out if code is running locally from Visual Studio / Cassini

I have an interface, call it ILocateLogFile with a standard implementation for dev / beta / production servers and one that works only in the local development environment. It seems I don’t think of a good clean solution method (preferably at compile time, but the runtime will be fine) if I run locally or on the server. This is a WCF application hosted on IIS, if that matters.

The best I came up with is to use a compiler symbol, for example:

  ILocateLogFile locateLogFile; #if DEBUG locateLogFile = new DevSandboxLogFileLocator(); #else locateLogFile = new LogFileLocator(); #endif 

The problem is that compiled characters are set by an assembly that I do not control, and I want to be sure. Is there any automated way to check for the presence of Visual Studio? Or at least check out Cassini, not IIS?

+6
source share
3 answers

Two ways I did it 1, you can check the process name

 bool isRunningInIisExpress = Process.GetCurrentProcess() .ProcessName.ToLower().Contains("iisexpress"); 

Or update your configuration file using custom setup

 <appSettings> <add key="ApplicationEnvironment" value="LOCAL_DEV" /> </appSettings> 

What do you specifically update for each environment and have an application request for

I'm not sure if there is a way to determine this at compile time, in addition to having a special build configuration for each environment and placing a custom PRAGMA for each of these collections. Personally, I think this is not so elegant, but it can also work.

+8
source

I found it here and it worked for me-. Determine if the ASP.NET application is running locally.

 bool isLocal = HttpContext.Current.Request.IsLocal; 
0
source

Here is the code I used

 If Debugger.IsAttached Then ' Since there is a debugger attached, ' assume we are running from the IDE Else ' Assume we aren't running from the IDE End If 
-2
source

Source: https://habr.com/ru/post/946031/


All Articles