Windows Azure Environment.GetEnvironmentVariable("RoleRoot") read access denied error
I have an xml file that I ma tryin to read from
string xmlTemplatePath = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"\", @"approot\myxml.xml");
Using XDocument doc = XDocument.Load(xml开发者_如何学编程TemplatePath)
, however I get an access denied exception both on the cloud and development environment. Any ideas why a read is denied?
I know, old thread... but the answer could be useful to someone nevertheless.
It appears that an azure worker process runs under a custom user (shows up as a GUID in taskmgr). This user account appears to be quite restricted and has no read-access to the filesystem (even inside the approot directory).
My solution was to create a startup script for the azure role (see msdn article) which uses icacls to add permissions to access the files. In this case, I created a file called startup\Install.cmd in my project, containing the following:
@echo off
if "%EMULATED%"=="true" goto :EOF
echo Allowing access to files
cd directory_where_files_exist
icacls *.* /grant Everyone:F
In my ServiceDefinition.csdef file, I instruct the Azure deployment system to run this startup script during deployment by putting this inside the role definition (e.g. <WorkerRole>):
<Startup>
<Task executionContext="elevated" commandLine="startup\Install.cmd">
<Environment>
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>
</Environment>
</Task>
</Startup>
The variable "EMULATED" is set when the emulator is running - note that in the startup/Install.cmd file above, I skip doing the permission change if running in the emulator.
Looks fine to me. I even grabbed your code and dropped it into a worker role, calling it from OnStart(), after creating myxml.xml in my project root, just to make sure I wasn't missing something obvious.
Check myxml.xml properties: "Build Action" should be "Content" and "Copy to Output Directory" should be "Copy always" or "Copy if newer". If that doesn't fix it, check permissions on that file.
精彩评论