Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an interface, let's call it ILocateLogFile, with a standard implementation for dev/beta/production servers, and one that only works in the local development environment. I can't seem to think of a nice clean way to decide (preferably at compile time, but runtime would be fine) if I'm running locally or on a server. This is a WCF application hosted on IIS, if that matters.

The best I've come up with is to use a compiler symbol, something like:

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

The problem is, compile symbols get set by the build, which I don't control, and I want to be certain. Isn't there some automagical way to check for the presence of Visual Studio? Or at least to check for Cassini rather than IIS?

like image 690
McGarnagle Avatar asked May 28 '13 17:05

McGarnagle


1 Answers

Two ways I have done this 1 you can check the process name

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

Or update your config file with a custom setting

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

That you update specifically for each environment and have you application query for

I'm not sure if there is a way to determine this at compile time, besides having a special build configuration that is for each environment and putting a custom PRAGMA for each of these builds. Personally I think that is not as elegant, but it could also work.

like image 71
jstromwick Avatar answered Oct 10 '22 07:10

jstromwick