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
When I tried the "RoleRoot" environment variable in a web role, it returned null, unfortunately breaking the elegant solution shown above. Perhaps Microsoft changed something since 2013 or the solution is only valid for worker roles, not web roles.
The alternative below I saw working properly for a from-the-box configured webrole (not running elevated). Although the role is by default running as "network service", it can detect the presence of "f:\RoleModel.xml". Probably that is required because the configuration file contains information required in the role startup code. Note that the code does not depend on the actual drive letter, that may change in future Azure images:
///
/// Returns true if the application is detected to be running in an Azure role (tested for web roles).
///
public static bool RunningInAzure
{
get
{
try
{
string sCurrentDrive = Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory);
if (!string.IsNullOrEmpty(sCurrentDrive))
return File.Exists(Path.Combine(sCurrentDrive, "RoleModel.xml"));
}
catch { }
return false;
}
}
Tested for a web role, but I'd expect it to work the same for worker roles (please comment if it doesn't).