1
votes

I'm trying to run my Azure Application using Azure emulator. I have a worker role added and I've overridden the method Run().

In my code, I need the physical path of the worker role or the project.

I tried to use Environment.GetEnvironmentVariable("TempFolder") and HttpRuntime.AppDomainAppPath but both returned null.

Do you have any advice to get the physical path on worker role Run()?

2

2 Answers

1
votes

Worker Roles are not persisting temporary folders and it is not recommended to use them. For local storage you should use Local Resources. You must configure these resources in advance.

And to get the actual folder path, use this code:

RoleEnvironment.GetLocalResource("ResourceName").RootPath;

Where "ResourceName" is a name of a configured Local Resource.

A bit more description here

0
votes

The physical folder / path of Azure Worker roles can be accessed using the RoleRoot Environment variable - this works for both local deployments (ie running in Debug) and on Azure itself. Note that all content added to your Worker Roles via Visual Studio go under an AppRoot folder.

For example, if you had this in Visual Studio:

MyWork.AzureWorkerProject
-- Roles
---- MyWork.AzureWorkerProject.WorkerRole
------ images
-------- test.png

The following code should return you the correct file path for test.png, regardless of whether you're running in dev locally or in the cloud.

string appRoot = Environment.GetEnvironmentVariable("RoleRoot");   
string fullPath = Path.Combine(appRoot + @"\", @"AppRoot\images\test.png");

Further reading if you're interested can be found here and here.