azure – 如何从RoleEntryPoint.OnStart()获取WebRole站点根路径?

前端之家收集整理的这篇文章主要介绍了azure – 如何从RoleEntryPoint.OnStart()获取WebRole站点根路径?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
作为在 Windows Azure上启动WebRole的一部分,我想访问正在启动的网站上的文件,我想在RoleEntryPoint.OnStart()中执行此操作.这将使我能够在ASP.NET AppDomain加载之前影响ASP.NET配置.

当使用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;

如何从一个可以在开发人员和Azure上工作的方式获取来自RoleEntryPoint.OnStart()的WebRole站点根路径?

解决方法

这似乎在开发和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);
    }
}

如果任何人使用它来操纵ASP.NET应用程序中的文件,您应该知道由RoleEntryPoint.OnStart()编写的文件将具有阻止ASP.NET应用程序更新的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);
原文链接:https://www.f2er.com/html/224299.html

猜你在找的HTML相关文章