.net - 我可以从 Azure Web 角色入口点访问 web.config 吗?

标签 .net web-config azure settings azure-web-roles

我有一个 Azure Web 角色,我想在 <appSettings> 下的 web.config 中存储一些设置标签。是的,我知道服务配置文件,但我有理由更喜欢 web.config。

当我执行时(来自 here ):

System.Configuration.Configuration rootWebConfig =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
if (rootWebConfig1.AppSettings.Settings.Count > 0) {
}

尽管我在 <appSettings> 下添加了键值对,但设置计数始终为零.

我做错了什么?是否可以从 Web 角色入口点内部读取 web.config 的设置?

最佳答案

原因是 Microsoft 自 Azure SDK 1.3 起引入了完整的 IIS 功能。 这样做的副作用是 RoleEntryPoint 与 Web 应用程序的其余部分隔离。

以下摘录自 Microsoft blog post描述了您所看到的内容。

...with full IIS, the RoleEntryPoint runs under WaIISHost.exe, while the web site runs under a normal IIS w3wp.exe process.

...so it expects its configuration to be in a file called WaIISHost.exe.config. Therefore, if you create a file with this name in the your web project and set the "Copy to Output Directory" property to "Copy Always" you'll find that the RoleEntryPoint can read this happily.

除了提到的解决方案之外,一个选项可能是尝试使用托管 Web 核心 (HWC) 模式而不是完整的 IIS 模式。

<小时/>

更新 - Azure SDK 1.8 中引入的更改

  • Azure SDK 1.3 -1.7 将在 WaIISHost.exe.config 中查找

  • Azure SDK 1.8+ 将在 WebRoleProjectName.dll.config 中查找。

通过对 SDK 的最新更改,您应该能够在项目中放置 app.config,然后您的角色入口点应该可以访问它。

关于.net - 我可以从 Azure Web 角色入口点访问 web.config 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571208/

相关文章:

debugging - IIS Express中的ASP.NET Core虚拟目录

azure - 使用可用区在 Azure 服务总线上实现高可用性

azure - Azure Function Apps 中全局变量的替代方案是什么?

Azure 示例 - WebApp-WSFederation-DotNet

c# - 登录表单问题

c# - 如何使用 aspose.pdf for .net 从头开始​​创建空白 pdf?

c# - 使用 WMI c# 控制 Microsoft NLB

iis-7 - 使用 web 配置从 url 中删除 index.cfm

c# - 获取远程主机的MAC地址

c# - 使用 configurationmanager 读取多个 web.config 文件