Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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

bool isLocal = HttpContext.Current.Request.IsLocal;

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top