2010-12-03 3 views
9

Windows Azure에서 WebRole을 시작하는 과정에서 시작되는 웹 사이트의 파일에 액세스하고 싶습니다.이 작업을 RoleEntryPoint.OnStart()에서하고 싶습니다. 이것은 예를 들어 ASP.NET AppDomain이로드되기 전에 ASP.NET 구성에 영향을 줄 수 있습니다.RoleEntryPoint.OnStart()에서 WebRole 사이트 루트 경로를 얻으려면 어떻게해야합니까?

Azure SDK 1.3 및 VS2010을 사용하여 로컬에서 실행하는 경우 아래 샘플 코드는 그 트릭을 수행하지만이 코드는 해킹의 악취를 가지고 있으며 Azure로 배포 할 때 트릭을 수행하지 않습니다.

XNamespace srvDefNs = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"; 
    DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); 
    string roleRoot = di.Parent.Parent.FullName; 
    XDocument roleModel = XDocument.Load(Path.Combine(roleRoot, "RoleModel.xml")); 
    var propertyElements = roleModel.Descendants(srvDefNs + "Property"); 
    XElement sitePhysicalPathPropertyElement = propertyElements.Attributes("name").Where(nameAttr => nameAttr.Value == "SitePhysicalPath").Single().Parent; 
    string pathToWebsite = sitePhysicalPathPropertyElement.Attribute("value").Value; 

어떻게 모두 dev에와 푸른 작업 방식으로 RoleEntryPoint.OnStart()에서 WebRole 사이트 루트 경로를 얻을 수 있나요? 당신이 찾고있는 무엇을 줄

Environment.GetEnvironmentVariable("RoleRoot") 

않습니다

답변

12

이 윈도우 Azure 모두 dev에와 작업하는 것 :

private IEnumerable<string> WebSiteDirectories 
{ 
    get 
    { 
     string roleRootDir = Environment.GetEnvironmentVariable("RdRoleRoot"); 
     string appRootDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); 

     XDocument roleModelDoc = XDocument.Load(Path.Combine(roleRootDir, "RoleModel.xml")); 
     var siteElements = roleModelDoc.Root.Element(_roleModelNs + "Sites").Elements(_roleModelNs + "Site"); 

     return 
      from siteElement in siteElements 
      where siteElement.Attribute("name") != null 
        && siteElement.Attribute("name").Value == "Web" 
        && siteElement.Attribute("physicalDirectory") != null 
      select Path.Combine(appRootDir, siteElement.Attribute("physicalDirectory").Value); 
    } 
} 

사람이 ASP.NET 응용 프로그램에서 파일을 조작하려면이 옵션을 사용하는 경우, 당신이 알아야 할 그 RoleEntryPoint.OnStart에 의해 작성된 파일() ASP.NET 응용 프로그램이 ACL을 업데이트하지 못하도록 ACL 설정을 갖습니다. 당신은 ASP.NET에서 이러한 파일이 가능 있도록 파일 권한을 변경하는 방법이 코드 쇼를 작성해야하는 경우

는 : 참고

SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
IdentityReference act = sid.Translate(typeof(NTAccount)); 
FileSecurity sec = File.GetAccessControl(testFilePath); 
sec.AddAccessRule(new FileSystemAccessRule(act, FileSystemRights.FullControl, AccessControlType.Allow)); 
File.SetAccessControl(testFilePath, sec); 
+1

권한을 변경하거나 배포 된 파일을 편집하는 것은이 를 ServiceDefinition.csdef의 WebRole에 추가하는 경우에만 가능합니다. –

4

에서보세요?

+0

을, 나는이 SDK 1.5/1.6로 사라 생각합니다. – Nariman

+0

상수가'RdRoleRoot' 인 것처럼 보입니다. – sharptooth

+0

이제는 RdRoleRoot도 없습니다. – Amit

관련 문제