U4-2179 - Error with GetChildNodes after upgrade to Umbraco 6.0.5 from Umbraco 6.0.0

Created by Brendan McGill 02 May 2013, 06:52:33 Updated by Sebastiaan Janssen 06 May 2013, 17:17:49

When calling GetChildNodes on a node like so: var result = brandsNode.GetChildNodes(x => x.Name.Contains("keyword"));

I recieve this exception: Unable to cast object of type 'System.Func2[umbraco.NodeFactory.Node,System.Boolean]' to type 'System.Func2[umbraco.interfaces.INode,System.Boolean]'.

This line used to work fine in Umbraco 6.0.0, but now it fails in 6.0.5

I use this method in multiple places in our project, am I using this method incorrectly or has something related to this changed between 6.0.0 and 6.0.5? I'd like to check before I update all the calls in my project.

From the top of the stack trace: [InvalidCastException: Unable to cast object of type 'System.Func2[umbraco.NodeFactory.Node,System.Boolean]' to type 'System.Func2[umbraco.interfaces.INode,System.Boolean]'.] umbraco.NodeExtensions.GetChildNodes(Node node, Func`2 func) +49


Bjørn Fridal 04 May 2013, 09:57:33

I get similar errors after upgrading from 4.11.5 to 4.11.8

One example looks like this:

var category = uQuery.GetCurrentNode(); var children = category.GetDescendantNodes(p => p.NodeTypeAlias == "Product");

And the error message like this:

Error Loading Razor Script (file: Product.List) Unable to cast object of type 'System.Func2[umbraco.NodeFactory.Node,System.Boolean]' to type 'System.Func2[umbraco.interfaces.INode,System.Boolean]'. at umbraco.NodeExtensions.GetDescendantNodes(Node node, Func2 func) at ASP._Page_macroScripts_product_list_cshtml.Execute() in c:\www\macroScripts\product\list.cshtml:line 17 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable1 executors) at System.Web.WebPages.WebPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage) at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)

I was looking through the updates and came across this: http://issues.umbraco.org/issue/U4-1754 Could it be related? It mentions umbraco.NodeExtensions, GetChildNodes and looks like it fits with the versions.

Cheers Bjørn

Morten Christensen 04 May 2013, 10:03:50

Does sound a lot like its related to U4-1754. Thanks for adding more info.

Brendan McGill 04 May 2013, 11:15:08

I had a look around the Umbraco source, I think the problem might be in: Umbraco.Web\umbraco.presentation\umbraco\uQuery\NodeExtensions.cs

When you call GetChildNodes it attempts to cast a Func<Node, bool> to a Func<INode, bool>.

public static IEnumerable GetChildNodes(this Node node, Func<Node, bool> func) { return GetChildNodes((INode)node, (Func<INode, bool>) func).Cast(); }

    public static IEnumerable<INode> GetChildNodes(this INode node, Func<INode, bool> func)
        foreach (INode child in node.ChildrenAsList)
            if (func(child))
                yield return child;

It looks like this was changed in recent versions to also have a method for using GetChildNodes on INode's. GetDescendantNodes was also changed in the same way, which would explain why Bjørn has seen a similar exception.

Before the changes GetChildNodes did not cast anything:

public static IEnumerable GetChildNodes(this Node node, Func<Node, bool> func) { foreach (Node child in node.Children) { if (func(child)) { yield return child; } } }

Sebastiaan Janssen 06 May 2013, 14:07:44

Fixed in changeset 78eb91886dc8

Bjørn Fridal 06 May 2013, 14:16:34

Cool any news on when it will be fixed in 4.11.X ?

Sebastiaan Janssen 06 May 2013, 14:31:16

Ehm, right now :) Forgot that this code was in 4 as well! :-)

Sebastiaan Janssen 06 May 2013, 14:34:29

Also fixed in 4.11.9, changeset a1d7b0343530

Bjørn Fridal 06 May 2013, 14:57:02

Sweet. Does that mean that it will be possible to download it from the nightly, or is that not how it works?

Sebastiaan Janssen 06 May 2013, 15:06:28

@Bjørn yes, the nightly will be available soon (build 32, it's running now).

Bjørn Fridal 06 May 2013, 16:23:42

Excellent. Just downloaded and it looks to be working :)

Sebastiaan Janssen 06 May 2013, 17:17:49

@Bjørn a word of warning: the nightlies do NOT yet include the security fixes of the past week, do not go live with these nightlies. The fixes will be added very shortly before the release date (May 30th).

Priority: Normal

Type: Bug

State: Fixed


Difficulty: Normal


Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version: 6.0.6, 4.11.9


Story Points: