Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ian 23 posts 113 karma points
    Sep 26, 2016 @ 14:04
    Ian
    0

    "Destination array was not long enough" occasionally thrown

    Hi there,

    I'm getting a strange exception thrown seemingly at random - sometimes it works and sometimes exception thrown. (Running Umbraco version 7.4.1 assembly: 1.0.5891.23238)

    The line of code (in surface controller) seems simple enough:

    var requestSampleNode = home
    .Descendants("requestSampleCustom")
    .Where(n => n.Name.StartsWith(customFormIdentifier, StringComparison.OrdinalIgnoreCase))
    .FirstOrDefault();
    

    where 'home' is set correctly, and at least one document of type requestSampleCustom exists in the tree.

    The exception that is sometimes thrown on that line is:

    Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
    System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
       at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
       at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
       at System.Collections.Generic.List`1.CopyTo(T[] array, Int32 arrayIndex)
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       at Umbraco.Web.PublishedContentExtensions.<EnumerateDescendants>d__55.MoveNext()
       at Umbraco.Web.PublishedContentExtensions.<EnumerateDescendants>d__4c.MoveNext()
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at MyCompany.Umbraco.Forms.Controllers.RequestSampleController.HandleCustomRequestSample(CustomRequestSampleViewModel viewModel) in d:\Projects\MyCompany.Umbraco\MyCompany.Umbraco.Forms\Controllers\RequestSampleController.cs:line 428
    

    Ive googled for an answer to this but to no avail. (It almost looks like a threading issue to me)

    Can anyone help?

    tia

    Ian

  • Ian 23 posts 113 karma points
    Dec 09, 2016 @ 10:16
    Ian
    0

    For anyone finding this same sort of issue, the solution for me was to change the code to target the doctype more specifically:

    //THIS CAUSES EXCEPTIONS DEEP DOWN IN UMBRACO/LINQ:
    //var requestSampleNode2 = home.Descendants("requestSampleCustom")
    //    .Where(n => n.Name.StartsWith(customFormIdentifier,
    //        StringComparison.OrdinalIgnoreCase))
    //    .FirstOrDefault();
    //THIS WORKS:
            var forms = home.FirstChild(n => n.DocumentTypeAlias == "Forms");
            var nodes = forms
                          .Children(n => n.Name.StartsWith(customFormIdentifier, 
                            StringComparison.OrdinalIgnoreCase));
            if (nodes.Any())
            {
                var requestSampleNode = nodes.First();
                ...
    

    Hope it helps!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 09, 2016 @ 13:17
  • Ian 23 posts 113 karma points
    Dec 10, 2016 @ 22:18
    Ian
    0

    Thanks Dan - that is a great article that I wish I'd read ages ago!

Please Sign in or register to post replies

Write your reply to:

Draft