每天几次,我的Web窗体应用程序将抛出以下异常。该应用程序有一个包含菜单的母版页。例外是:
01/10/13 23:57:10.481 ERROR 79 LonelyCache.Global - Exception occurred:
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Web.UI.WebControls.MenuItemCollection.MenuItemCollectionEnumerator.MoveNext()
at System.Web.UI.WebControls.Menu.MenuRendererStandards.RenderItems(HtmlTextWriter writer, Boolean staticOnly, MenuItemCollection items, Int32 level, Boolean needsAccessKey)
at System.Web.UI.WebControls.Menu.MenuRendererStandards.RenderContents(HtmlTextWriter writer, Boolean staticOnly)
at System.Web.UI.WebControls.Menu.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.geocacherpage_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\9b853d2b\87164de6\App_Web_qxpnvss5.10.cs:line 0
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
母版页上的菜单是唯一的菜单,所以我认为必须将例外情况与它绑定。
该异常意味着菜单在被迭代时被更改。我唯一更改菜单的代码是清除菜单,然后在加载母版页时构建它。至少这是我的理解。
这是一个间歇性的问题。也就是说,它只在生产网站上每天发生几次。我已经看到有时菜单没有正确显示(某些项目丢失)。我认为这是发生此异常的结果。
有没有人对可能导致这种情况发生的原因有任何想法?
以下是我所拥有的与菜单相关的所有代码。
从Site.Master文件:
<div class="menuSide">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" />
</div>
来自Site.Master.cs文件:
public partial class SiteMaster : System.Web.UI.MasterPage
{
private static readonly ILog log = LogManager.GetLogger(typeof(SiteMaster));
public class NavMenu
{
private readonly bool anonymous;
private readonly string role;
private readonly MenuItem menuItem;
public NavMenu(bool anon, string rol, MenuItem item)
{
anonymous = anon; role = rol; menuItem = item;
}
public bool Anonymous { get { return anonymous; } }
public string Role { get { return role; } }
public MenuItem Item { get { return menuItem; } }
}
static readonly NavMenu[] menu =
{
new NavMenu(true, null, new MenuItem("HOME", "", "", "~/")),
new NavMenu(false, null, new MenuItem("PROFILE", "", "", "~/Account/Profile.aspx")),
new NavMenu(true, null, new MenuItem("FORUM", "", "", "~/forum")),
new NavMenu(true, null, new MenuItem("ABOUT", "", "", "~/About.aspx")),
new NavMenu(false, "Administrators", new MenuItem("ADMIN", "", "", "~/Admin/Admin.aspx")),
};
private void buildMenu()
{
NavigationMenu.Items.Clear();
foreach (var item in menu)
{
if (item.Anonymous ||
(HttpContext.Current.User.Identity.IsAuthenticated &&
(item.Role == null || HttpContext.Current.User.IsInRole(item.Role))))
{
NavigationMenu.Items.Add(item.Item);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
buildMenu();
// More stuff follows...
}
}