问题
Using MVC3 and I'd like to determine if I am running locally or deployed to the cloud?
回答1:
This is what I use
public static class Azure
{
private static bool m_IsRunningAzure = GetIsRunningInAzure();
private static bool GetIsRunningInAzure()
{
Guid guidId;
if (RoleEnvironment.IsAvailable && Guid.TryParse(RoleEnvironment.DeploymentId, out guidId))
return true;
return false;
}
public static bool IsRunningInAzure()
{
return m_IsRunningAzure;
}
private static bool m_IsRunningAzureOrDevFabric = GetIsRunningInAzureOrDevFabric();
private static bool GetIsRunningInAzureOrDevFabric()
{
return RoleEnvironment.IsAvailable;
}
public static bool IsRunningInAzureOrDevFabric()
{
return m_IsRunningAzureOrDevFabric;
}
}
回答2:
RoleEnvironment.IsAvailable
tells you if you're running in Windows Azure, but it doesn't differentiate between the real Windows Azure and the local development simulator.
I wrote a blog post that shows a trick to figure out whether you're running in real vs. simulated Windows Azure, when RoleEnvironment.IsAvailable == true
- hopefully that provides what you're looking for.
EDIT: In case you want the down-n-dirty code I describe in the abovementioned post, without any explanation of why the technique works:
private bool IsRunningInDevFabric()
{
// easiest check: try translate deployment ID into guid
Guid guidId;
if (Guid.TryParse(RoleEnvironment.DeploymentId, out guidId))
return false; // valid guid? We're in Azure Fabric
return true; // can't parse into guid? We're in Dev Fabric
}
EDIT 2: My answer is a bit outdated. There's now RoleEnvironment.IsEmulated
, which is much more straightforward to use. MSDN documentation is here
回答3:
You can do it the old-fashioned way, by looking for the existence of an Environment Variable.
Set the value of your environment variable in computer properties and read it using Environment.GetEnvironmentVariable("MyVariable").
On Azure, the variable won't be present, so the call will return null.
回答4:
There are a few suggestions here - http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/8fd96850-7a04-401b-89d5-ba153c1b4c51
- Environment variable
- deploymentID
- computer name
- Windows Azure Storage service endpoint
Looking at them, I think I'd be tempted to look at the AZURE_DRIVE_DEV_PATH environment variable - but there's no guarantee that this will work in future SDK versions.
来源:https://stackoverflow.com/questions/6160947/how-can-i-determine-if-i-am-running-locally-on-my-pc-or-on-the-cloud