Apparent Cache Issue

Sep 17, 2010 at 6:18 AM
Edited Sep 17, 2010 at 6:32 AM

There seems to be some issue we are having at the moment with the ASP.NET MVC SiteMap provider when it attempts to render after a certain time.

 

This is the error we are getting when attempting to render the site map after leaving the page for a couple of minutes:

 

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Source Error:

Line 9:      <div id="menu">
Line 10:         <div id="breadcrumbs">
Line 11:             <%=Html.MvcSiteMap().SiteMapPath(" &gt; ") %>
Line 12:         </div>
Line 13:         <div class="content">


Source File: C:\Website\Views\Shared\SiteMasterHeading.ascx    Line: 11

Stack Trace:

[ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.]
   System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark) +0
   System.Reflection.Assembly.GetTypes() +105
   MvcSiteMapProvider.DefaultSiteMapProvider.ProcessNodesInAssembly(Assembly assembly) in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:550
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:407

[MvcSiteMapException: An error occured while building the sitemap..]
   MvcSiteMapProvider.DefaultSiteMapProvider.BuildSiteMap() in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:439
   System.Web.StaticSiteMapProvider.FindSiteMapNode(String rawUrl) +133
   MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext context) in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\DefaultSiteMapProvider.cs:806
   System.Web.SiteMapProvider.get_CurrentNode() +35
   System.Web.SiteMap.get_CurrentNode() +34
   MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String separator, String separatorCssClass, String linkCssClass, Boolean currentNodeAsLink, Object htmlAttributes) in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\SiteMapPathHelper.cs:96
   MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String separator) in C:\Projects\Codeplex\TFS10\mvcsitemap\Main\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\SiteMapPathHelper.cs:40
   ASP.views_shared_sitemasterheading_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\Website\Views\Shared\SiteMasterHeading.ascx:11
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266



Version Information: Microsoft .NET Framework Version:2.0.50727.4206; ASP.NET Version:2.0.50727.4205



Upon increasing the cacheDuration I don't receive the error for a while longer, leaving me to believe there is an issue with the reflection the second time.  Is there a reason the reflection would stop working after the default time, or am I just missing something here?

Sep 23, 2010 at 4:40 AM

Any ideas at all?

This is a major roadblocker for me at the moment.

Coordinator
Sep 23, 2010 at 4:34 PM

Is there an option to get a repro version of your code?

Sep 24, 2010 at 1:22 AM

No unfortunately not.

It just seems as though the second time it needs to load the site map (after the cache expires) it dies.

Sep 27, 2010 at 7:39 AM

Okay I have the answer!

In the DefaultSiteMapProvider, there is a method called ProcessNodesInAssembly.

You had the following code:

            // Add all types
            foreach (Type type in assembly.GetTypes())
            {
                try
                {
                    var attribute =
                        type.GetCustomAttributes(typeof (IMvcSiteMapNodeAttribute), true).FirstOrDefault() as
                        IMvcSiteMapNodeAttribute;
                    if (attribute != null)
                    {
                        assemblyNodes.Add(new MvcSiteMapNodeAttributeDefinitionForController
                                              {
                                                  SiteMapNodeAttribute = attribute,
                                                  ControllerType = type
                                              });
                    }
                }
                catch
                {
                }

As soon as I change it to the following it works:

            Type[] types;
            try
            {
                types = assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException ex) {
                types = ex.Types;
            }
            // Add all types
            foreach (Type type in types)
            {
                if (type == null) {
                    continue;
                }

                try {
                    var attribute =
                        type.GetCustomAttributes(typeof (IMvcSiteMapNodeAttribute), true).FirstOrDefault() as
                        IMvcSiteMapNodeAttribute;
                    if (attribute != null) {
                        assemblyNodes.Add(new MvcSiteMapNodeAttributeDefinitionForController {
                                                                                                 SiteMapNodeAttribute = attribute,
                                                                                                 ControllerType = type
                                                                                             });
                    }
                }
                catch 
                {
                }

Coordinator
Oct 1, 2010 at 12:23 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.