I'm moving the deployment of a web app from an Azure Website into a Web Role in a Cloud Service.
Part of the migration has involved reserving some local storage in the role config and changing interactions with the local file-system to use the following mantra to find a path that is good for writing to:
LocalResource tempStorageResource = RoleEnvironment
.GetLocalResource("SomeRoleStorage");
var targetFolderPath = tempStorageResource.RootPath;
However, I'd like to keep things working in the WebSite instance. I'm going to write a path provider that abstracts the actual location away. Part of implementing this will require detecting whether I'm running locally/in the debugger, but I also need to know whether the running code is running under a WebSite or a WebRole. How can I do this?
public class AzurePathProvider : ILocalStoragePathProvider
{
public string GetStoragePath(string key)
{
var isWebRole = //????;
if(isWebRole)
{
LocalResource tempStorageResource =
RoleEnvironment
.GetLocalResource(key);
return tempStorageResource.RootPath;
}
else
{
return "/some/other/storage/location";
}
}
}
Check for RoleEnvironment.IsAvailable
to decide if the code is running in Cloud Service or not. It will always be true
when your code is running in Cloud Service otherwise it will be false
.
Furthermore to detect if the code is running in compute emulator, you can check for RoleEnvironment.IsEmulated
along with RoleEnvironment.IsAvailable
.