"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)
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();
...
"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:
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:
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
For anyone finding this same sort of issue, the solution for me was to change the code to target the doctype more specifically:
Hope it helps!
Have a read of this -> https://our.umbraco.org/documentation/Reference/Common-Pitfalls/ :)
Thanks Dan - that is a great article that I wish I'd read ages ago!
is working on a reply...