I have some shared assemblies/projects that are used within Winforms apps, windows services and now Azure worker roles.
Is there any way that I can detect at runtime
For anyone interested, thought I would sharehow I implemented, thanks to @Sandrino Di Mattia's answer:
You can check for the presence of the RoleRoot environment variable (for Cloud Services at least):
Note that this does NOT cater for a Winforms App as I only actually required it in the end for Services - i.e. detecting between service running as
This is an outline:
public static class ServiceRunner
{
private static bool IsAzureWorker
{
get { return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("RoleRoot")); }
}
public static void Run(string[] args)
{
if (IsAzureWorker)
{
//Running as Azure Worker
}
else if (Environment.UserInteractive) //note, this is true for Azure emulator too
{
//Running as Console App
}
else
{
//Running as Windows Service
}
}
}