9

Windows Azure で WebRole を起動する一環として、起動中の Web サイトのファイルにアクセスしたいと考えており、これを 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 サイトのルート パスを取得するにはどうすればよいですか?

4

2 に答える 2

12

これは、開発環境と 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);
于 2010-12-06T03:35:40.220 に答える
4

を見てみましょう:

Environment.GetEnvironmentVariable("RoleRoot")

それはあなたが探しているものをあなたに与えますか?

于 2010-12-03T13:44:22.823 に答える