Cached sitemap not clearing?

Sep 30, 2010 at 11:59 AM

Hello,

I've been noticing for some time an issue with the sitemap where, under certain seemingly random circumstances, single items would disappear from my sitemap.  I couldn't seem to find any trigger for this issue, other than it seeming to occur around the same time an exception might be thrown elsewhere in the application.  After investigation, I found that the problem occurred whenever I forced the sitemap to rebuild itself, by skipping this ifcheck in DefaultSitemapProvider:

        public override SiteMapNode BuildSiteMap()
        {
            if (root != null && (HttpContext.Current.Cache[cacheKey] != null || isBuildingSiteMap))
            {

This then called Clear() and reset the sitemap.  However, what I found was at this point (every time), one of the nodes of the sitemap would fail to be added, and an exception would be thrown stating that a duplicate URL already existed in the sitemap.  If I then interrogated the sitemap, however, the node I was attempting to add did not exist. 

What seems to be happening is a reference sometimes persists in the underlying StaticSitemapProvider's URLTable object, and this doesn't get cleared properly.  The solution I have implemented, which seems to solve the issue, is to manually remove the node before adding it if a reference exists in the URLTable:

       protected override void AddNode(SiteMapNode node, SiteMapNode parentNode)
        {
            try
            {
                // Avoid issue with url table not clearing correctly.
                if (base.FindSiteMapNode(node.Url) != null)
                {
                    if (parentNode != null) base.RemoveNode(parentNode);
                    base.RemoveNode(node);
                }

                base.AddNode(node, parentNode);
                
            }
            catch (InvalidOperationException)
            {
                if (!isBuildingSiteMap)
                {
                    throw;
                }
            }
        }

Has anyone else encountered a similar issue, or found a better fix for this solution?  The problem seems to occur in every version of the code that I tried (2.0 and onwards).


Coordinator
Oct 1, 2010 at 10:59 AM

I think you just found a solution to a problem we've been struggling with a long time. Thanks!

Coordinator
Oct 1, 2010 at 11:02 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.