You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4部署Azure App Service后数组索引越界报错求助

Azure App Service部署后读取web.config出现数组越界异常

问题代码

for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
        string key = cfg.AppSettings.Settings.AllKeys[i];  // 报错位置:第258行
        string value = cfg.AppSettings.Settings[key].Value;
        result.Add(key, value);
}

web.config配置段

<appSettings>
  <add key="EmailsLogged" value="True"/>
  <add key="EmailAdminNotification" value="True"/>
  <add key="EmailFrom" value="test@test.com"/>
  <add key="EmailAdminTo" value="test@test.com"/>
  <add key="DataEntryIsEnabled" value="True"/>
  <add key="ViewReportsIsEnabled" value="True"/>
  <add key="ViewOperationsIsEnabled" value="True"/>
  <add key="DownloadDataIsEnabled" value="True"/>
  <add key="ChangeDetailsIsEnabled" value="True"/>
  <add key="PasswordReclaimIsEnabled" value="True"/>
  <add key="RegistrationIsEnabled" value="True"/>
  <add key="SiteIsEnabled" value="True"/>
  <add key="Environment" value="dev"/>
  <add key="ForcePasswordChange" value="true"/>
  <add key="PasswordExpiryInDays" value="90"/>
  <add key="MaxInvalidPasswordAttempts" value="3"/>
  <add key="MinimumOperationDate" value="01/04/2015"/>
</appSettings>

错误信息

Index was outside the bounds of the array.
at AppConfiguration.GetAppSettings() in c:\home\site\wwwroot\App_Code\Configuration\AppConfiguration.cs:line 258
at Secure_CheckPassword.HasPasswordExpired() in c:\home\site\wwwroot\Secure\CheckPassword.aspx.cs:line 28
at Secure_CheckPassword.Page_Load(Object sender, EventArgs e) in c:\home\site\wwwroot\Secure\CheckPassword.aspx.cs:line 19
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

环境

项目基于.NET Framework 4,Visual Studio 2022本地运行、IIS部署运行均正常,发布至Azure App Service后触发错误。

排查与解决方案

核心问题

代码中循环条件使用ConfigurationManager.AppSettings.Count,但读取配置键时用的是cfg.AppSettings.Settings.AllKeys[i],两个对象的配置项计数不一致:

  • ConfigurationManager.AppSettings读取的是合并后的配置(web.config + Azure门户应用程序设置)
  • cfg若为直接读取本地web.config的Configuration实例,仅包含文件内的配置项

当Azure门户中新增/删除了应用程序设置时,两者的Count值出现差异,循环次数超过cfg.AppSettings.Settings.AllKeys的长度,触发数组越界。

修复方案

方案1:统一使用同一配置对象

修改循环条件与读取逻辑为同一配置实例:

// 用cfg实例的计数作为循环条件
for (int i = 0; i < cfg.AppSettings.Settings.Count; i++)
{
        string key = cfg.AppSettings.Settings.AllKeys[i];
        string value = cfg.AppSettings.Settings[key].Value;
        result.Add(key, value);
}

或全程使用ConfigurationManager:

for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
        string key = ConfigurationManager.AppSettings.AllKeys[i];
        string value = ConfigurationManager.AppSettings[key];
        result.Add(key, value);
}

方案2:改用foreach遍历(更安全)

直接遍历配置键集合,避免索引越界风险:

foreach (string key in cfg.AppSettings.Settings.AllKeys)
{
    string value = cfg.AppSettings.Settings[key].Value;
    result.Add(key, value);
}

额外检查

登录Azure门户,进入对应App Service的「配置」→「应用程序设置」,确认是否存在与web.config重复或额外的配置项,确保配置逻辑符合预期。

内容的提问来源于stack exchange,提问作者KevBarri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:05:25