Azure 如何从RoleEntryPoint.OnStart()获取WebRole站点根路径?
作为在Windows Azure上启动WebRole的一部分,我希望访问正在启动的网站上的文件,并希望在RoleEntryPoint.OnStart()中执行此操作。例如,这将使我能够在加载ASP.NET AppDomain之前影响ASP.NET配置 当使用Azure SDK 1.3和VS2010在本地运行时,下面的示例代码可以做到这一点,但代码周围有黑客的臭味,并且在部署到Azure时不会做到这一点Azure 如何从RoleEntryPoint.OnStart()获取WebRole站点根路径?,azure,azure-web-roles,Azure,Azure Web Roles,作为在Windows Azure上启动WebRole的一部分,我希望访问正在启动的网站上的文件,并希望在RoleEntryPoint.OnStart()中执行此操作。例如,这将使我能够在加载ASP.NET AppDomain之前影响ASP.NET配置 当使用Azure SDK 1.3和VS2010在本地运行时,下面的示例代码可以做到这一点,但代码周围有黑客的臭味,并且在部署到Azure时不会做到这一点 XNamespace srvDefNs = "http://schemas.microso
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;
如何从RoleEntryPoint.OnStart()获取WebRole站点根路径,使其在开发人员和Azure上都能工作?请看:
Environment.GetEnvironmentVariable("RoleRoot")
这是否为您提供了所需的功能?这似乎在开发人员和Windows Azure上都适用:
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);
}
}
只有在ServiceDefinition.csdef.FYI中将其添加到WebRole中,才能更改权限和/或编辑部署的文件。我认为这在SDK 1.5/1.6中消失了。看起来常量是
RdRoleRoot
。
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);