SiteMapProvider Exception Crashes the Website

on Saturday, April 25, 2009

Problem

I’m getting this error message from Visual Studio (The debugger cannot continue running the process. Process was terminated.):

image

The error message is appearing when debugging a ASP.NET MVC application in Visual Studio 2008. The lines of code which are being executed at taking a object tree and converting it to populate a SiteMapProvider. The error is occuring when stepping from line 10 to line 16. Line 16 is highlighted when error occurs:

Code Snippet 1 (CS1)

   1: private static InsideSaSiteMapNode CreateMvcNode( InsideSaSiteMapNodeDTO dto )
   2: {
   3:     var node                = new InsideSaSiteMapIntegrationNode( _Provider, dto.Href );
   4:     node.Href               = dto.Href;
   5:     node.Title              = dto.Title;
   6:     node.JavascriptAction   = dto.JavascriptAction;
   7:     node.IconFilename       = dto.IconFilename;
   8:     node.Target             = dto.Target;
   9:  
  10:     foreach( var child in dto.ChildNodes )
  11:     {
  12:         var childNode    = CreateMvcNode( child );
  13:         node.ChildNodes.Add( childNode );
  14:     }
  15:  
  16:     return node;
  17: }

As a bonus, the “Process” being referenced in the error message is the ASP.NET Development Server which is running the website. So, the website crashes.

Analysis

The error first started when I added a line of to the SiteMapProvider method, GetChildNodes. The new code is on line 11 (commented out).

CS2

   1: /// <summary>
   2: /// When overridden in a derived class, retrieves the child nodes of a specific <see cref="T:System.Web.SiteMapNode"/>.
   3: /// </summary>
   4: /// <returns>
   5: /// A read-only <see cref="T:System.Web.SiteMapNodeCollection"/> that contains the immediate child nodes of the specified <see cref="T:System.Web.SiteMapNode"/>; otherwise, null or an empty collection, if no child nodes exist.
   6: /// </returns>
   7: /// <param name="node">The <see cref="T:System.Web.SiteMapNode"/> for which to retrieve all child nodes. 
   8: /// </param>
   9: public override SiteMapNodeCollection GetChildNodes( SiteMapNode node )
  10: {
  11:     //return node.ChildNodes;
  12:     return null;
  13: }

I realize that your not supposed to use the incoming node to return the child values. But, that line of code has not been called yet in the program. The error occurs in line CS1:16. The value being returned was made by a call from CS1:12; which might mean that CS1:13 is “pre-executed” when the return value is known. The frustration really lies in the idea that node.ChildNodes (which returns a null value) causes an exception which crashes the web server while returning null produces an exception which is handled in the debugger.

Solution

As you can see in the code above, the offending line of code was commented out. But, it was a good lesson to learn about SiteMapProvider development. When calling the Add method on a SiteMapNode the provider’s abstract methods will get called; and they’re a bit touchy.


Technorati Tags: ,,,

0 comments:

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.