如何从 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 站点根路径?
As part of starting up a WebRole on Windows Azure I would like to access files on the website being started and I would like to do this in RoleEntryPoint.OnStart(). This will for instance enable me to influence ASP.NET config before the ASP.NET AppDomain is loaded.
When running locally with Azure SDK 1.3 and VS2010 the sample code below do the trick, but the code has the stench of hack around it and it does not do the trick when deploying to 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;
How can I get the WebRole site root path from RoleEntryPoint.OnStart() in a way that work in both dev and on Azure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎在开发和 Windows Azure 上都有效:
如果有人使用它来操作 ASP.NET 应用程序中的文件,您应该知道由 RoleEntryPoint.OnStart() 编写的文件将具有阻止 ASP.NET 应用程序的 ACL 设置从更新它们。
如果您需要从 ASP.NET 写入此类文件,此代码将显示如何更改文件权限,以便这是可能的:
This seem to work in both dev and on Windows Azure:
If anyone use this to manipulate files in the ASP.NET app, you should know that the files written by RoleEntryPoint.OnStart() will have ACL settings that prevent the ASP.NET application from updating them.
If you need to write to such files from ASP.NET this code show how you can change file permissions so this is possible:
看看:
这是否给了你你正在寻找的东西?
Take a look at:
Does that give you what you're looking for?