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.

SharpCompress 0.28.1 depends on System.Buffers 4.4.0, but NuGet transitively depends on 4.5.1

See original GitHub issue

Bug description

Using SharpCompress 0.28.1 in a .NET 4.6.2 unit test project I get the following exception:

System.IO.FileLoadException: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
    at SharpCompress.Utility.Skip(Stream source)
   at SharpCompress.Common.EntryStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at SharpCompress.Readers.AbstractReader`2.Write(Stream writeStream)
   at SharpCompress.Readers.AbstractReader`2.WriteEntryTo(Stream writableStream)
   at SharpCompress.Readers.IReaderExtensions.<>c__DisplayClass4_0.<WriteEntryToFile>b__0(String x, FileMode fm)
   at SharpCompress.Common.ExtractionMethods.WriteEntryToFile(IEntry entry, String destinationFileName, ExtractionOptions options, Action`2 openAndWrite)
   at SharpCompress.Readers.IReaderExtensions.WriteEntryToFile(IReader reader, String destinationFileName, ExtractionOptions options)
   at SharpCompress.Common.ExtractionMethods.WriteEntryToDirectory(IEntry entry, String destinationDirectory, ExtractionOptions options, Action`2 write)
   at SharpCompress.Readers.IReaderExtensions.WriteEntryToDirectory(IReader reader, String destinationDirectory, ExtractionOptions options)
   at ...

Analysis

Build output

The detailed MSBuild output of building the test project shows:

2>  Unified primary reference "System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51".
2>      Using this version instead of original version "4.0.2.0" in "C:\Users\xxx\.nuget\packages\sharpcompress\0.28.1\lib\netstandard2.0\SharpCompress.dll" because AutoUnify is 'true'.
2>      Resolved file path is "C:\Users\xxx\.nuget\packages\system.buffers\4.5.1\ref\net45\System.Buffers.dll".
2>      Reference found at search path location "{RawFileName}".
2>      Found related file "C:\Users\xxx\.nuget\packages\system.buffers\4.5.1\ref\net45\System.Buffers.xml".
2>      This reference is not "CopyLocal" because at least one source item had "Private" set to "false" and no source items had "Private" set to "true".
2>      The ImageRuntimeVersion for this reference is "v4.0.30319".

Dependency of SharpCompress 0.28.1 (.NET Standard 2.0) assembly

Disassembling the SharpCompress 0.28.1 assembly with ildasm shows that it indeed references "System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51":

.assembly extern System.Buffers
{
  .publickeytoken = (CC 7B 13 FF CD 2D DD 51 )                         // .{...-.Q
  .ver 4:0:2:0
}

Dependency of SharpCompress 0.28.1 NuGet package

The SharpCompress 0.28.1 NuGet package depends on System.Buffers in the following way: SharpCompress 0.28.1 → >= System.Memory 4.5.4 → >= System.Buffers 4.5.1

Signature of System.Buffers 4.4.0

Disassembling System.Buffers 4.4.0 NuGet package with ildasm, we get the following signature:

.assembly System.Buffers
{
  // snip
  .ver 4:0:2:0
}

Signature of System.Buffers 4.5.1

Disassembling System.Buffers 4.5.1 NuGet package with ildasm, we get the following signature:

.assembly System.Buffers
{
  // snip
  .ver 4:0:3:0
}

Conclusion

The .NET Standard 2.0 assembly of SharpCompress.dll which is contained in the SharpCompress 0.28.1 NuGet package depends on System.Buffers.dll which is contained in the System.Buffers 4.4.0 NuGet package (assembly version 4.0.2.0).

This creates a version conflict because the SharpCompress 0.28.1 NuGet package transitively depends on the System.Buffers 4.5.1 NuGet package (assembly version 4.0.3.0).

Workaround

I was able to work around this issue by forcing the System.Buffers NuGet version to 4.4.0:

<PackageReference Include="SharpCompress" Version="0.28.1" />
<PackageReference Include="System.Buffers" Version="[4.4.0]" />

The drawback is that it generates the following warning:

NU1605: Detected package downgrade: System.Buffers from 4.5.1 to 4.4.0. Reference the package directly from the project to select a different version. 
 MyProject.UnitTests -> SharpCompress 0.28.1 -> System.Memory 4.5.4 -> System.Buffers (>= 4.5.1) 
 MyProject.UnitTests -> System.Buffers (= 4.4.0)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
adamhathcockcommented, Apr 24, 2021

Downgrading to System.Memory 4.5.3 will be System.Buffers 4.4.0: https://github.com/adamhathcock/sharpcompress/pull/592

1reaction
quessithcommented, Apr 15, 2021

Thanks i couldnt figure out the issue untill i found your post! Cheers and thanks for that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly 'System.Buffers, Version= ...
I installed the latest System.Buffer nuget package v4.5.1, created dependentAssembly in my app.config and Reference in my .csproj file, but I ...
Read more >
System.Buffers 4.5.1
Provides resource pooling of any type for performance-critical applications that allocate and deallocate objects frequently.
Read more >
Could not load file or assembly 'System.Buffers' or one of ...
With assembly binding logging enabled, it appears the issue is a binding redirect expecting assembly version 4.0.3.0 , but finding 4.0.2.0 .
Read more >
MSB3277: Found conflicts between different versions of ' ...
This error occurs during a build when more than one version of the same dependent assembly is referenced in a build of the...
Read more >
Upgrading indirect NuGet dependencies - Tim Abell
Update - transitive pinning. It seems microsoft have produced a solution to this problem, along with the long awaited solution-level package ...
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