question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to compare two elements in the array. - Monotorrent v3.0.0-beta.rev0102

See original GitHub issue
System.InvalidOperationException
Message : Failed to compare two elements in the array.
StackTrace :    at System.Collections.Generic.GenericArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Array.BinarySearch[T](T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value)
   at MonoTorrent.BEncoding.BEncodeDecoder.DecodeTorrent(Stream reader)
   at MonoTorrent.Torrent.Load(Stream stream, ReadOnlySpan`1 buffer)
   at MonoTorrent.Torrent.Load(String path)
   at MultiUploader.Helpers.Torrents.IsImageFromTorrent(String torrentFile)
System.BadImageFormatException
Message : Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
StackTrace :    at System.ReadOnlyMemory`1.get_Span()
   at MonoTorrent.BEncoding.BEncodedString.CompareTo(BEncodedString other)
   at System.Collections.Generic.GenericArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value)
   at System.Collections.Generic.GenericArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
System.BadImageFormatException
Message : Cannot load a reference assembly for execution.

My Code is:

foreach (TorrentFile file in Torrent.Load(torrentFile)) {
         if (Regex.IsMatch(file.Path, "^.*\.(iso|cue)$", RegexOptions.IgnoreCase)) {
             return true;
         }
     }
 }
 return false;

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:24 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
alanmcgoverncommented, Dec 12, 2022

still_ref_assembly

I can definitely see the ref assembly listed here… but that shouldn’t happen 😦

0reactions
MSLukeWestcommented, Feb 9, 2023

@alanmcgovern @foxi69 I tried this scenario using the supplied project in TestAppInstaller.zip, and in my testing I found that both workarounds fix the issue where reference assemblies are taken from NuGet packages. Here’s what I did:

  1. Built the sample app as is, and saw that there were six reference assemblies being copied from NuGet packages:

%NugetCachePath%\system.buffers\4.5.1*ref*\net45\System.Buffers.dll %NugetCachePath%\system.drawing.common\5.0.3*ref*\net461\System.Drawing.Common.dll %NugetCachePath%\system.numerics.vectors\4.5.0*ref*\net46\System.Numerics.Vectors.dll %NugetCachePath%\system.runtime.compilerservices.unsafe\4.5.3*ref*\net461\System.Runtime.CompilerServices.Unsafe.dll %NugetCachePath%\system.security.principal.windows\4.0.0*ref*\net46\System.Security.Principal.Windows.dll %NugetCachePath%\system.valuetuple\4.5.0*ref*\net47\System.ValueTuple.dll

  1. Unzipped into a separate folder and converted the project back to packages.config. This took a while and involved manually adding each package back through NuGet Package Manager (there might be an easier way). After this I did a clean, then built, and I no longer see any reference assemblies coming from NuGet packages. I confirmed that those 6 assemblies are included, and they’re all the “real” versions:

TesterApp\packages\System.Buffers.4.5.1*lib*\net461\System.Buffers.dll TesterApp\packages\System.Drawing.Common.5.0.3*lib*\net461\System.Drawing.Common.dll TesterApp\packages\System.Numerics.Vectors.4.5.0*lib*\net46\System.Numerics.Vectors.dll TesterApp\packages\System.Runtime.CompilerServices.Unsafe.4.5.3*lib*\net461\System.Runtime.CompilerServices.Unsafe.dll TesterApp\packages\System.Security.Principal.Windows.4.0.0*lib*\net46\System.Security.Principal.Windows.dll TesterApp\packages\System.ValueTuple.4.5.0*lib*\net47\System.ValueTuple.dll

  1. Unzipped into another separate folder, and this time added the target from the comment in https://github.com/NuGet/Home/issues/9310 into the bottom of the csproj file, just above the closing Project tag. After this I did a clean, then built, and I see the same result, no reference assemblies came from Nuget packages:

%NugetCachePath%\system.buffers\4.5.1\lib\net461\System.Buffers.dll %NugetCachePath%\system.drawing.common\5.0.3\lib\net461\System.Drawing.Common.dll %NugetCachePath%\system.numerics.vectors\4.5.0\lib\net46\System.Numerics.Vectors.dll %NugetCachePath%\system.runtime.compilerservices.unsafe\4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll %NugetCachePath%\system.security.principal.windows\4.0.0\runtimes\win\lib\net46\System.Security.Principal.Windows.dll %NugetCachePath%\system.valuetuple\4.5.0\lib\net47\System.ValueTuple.dll

So, while I can’t confirm this makes the app work, I do think it’s worth revisiting these workarounds (specifically the much simpler one explained in 3 above) and taking a closer look at what’s failing.

A couple notes:

  1. I did this testing in a recent Visual Studio 2022 preview build, but I’d expect the released VS 2022 Update 4 or soon to be released Update 5 builds to show the same behavior.
  2. My testing was aided by this extension, which produces .binlog files for the build performed by VS: https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProjectSystemTools2022. You can see what files get added to the installer by seeing what gets added during the targets “BuiltProjectOutputGroup” and “BuiltProjectOutputGroupDependencies”.
  3. Make sure to do a Clean in VS, delete obj/bin folders, and clean the local solution package cache after doing the conversion
Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to compare two elements in the array
My objects don't implement IComarable or IComparable at all and they still work fine. I am creating a CollectionViewSource and adding sort ...
Read more >
error LGHT0001: Failed to compare two elements in the ...
The linker on v4.0.2220.0 throws this error: light.exe(0,0): error LGHT0001: Failed to compare two elements in the array. The bug did not exist...
Read more >
“Failed to compare two elements in the array” Error During ...
I've made a custom SPExport and SPImport tool that duplicates site content around the SharePoint site collection. However, it would suddenly ...
Read more >
Failed to compare two elements in the array. in UI for ...
This only happens when I try to adda new activity to the grid when it is sorted. When the Grid is not sorted,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found