SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] Wrong version of System.Buffers is requested

Open ziriax opened this issue 5 years ago • 4 comments

Description

I have a .NET 4.8 unit test project that references SkiaSharp 1.68.3.

Some unit tests fail with:

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 SkiaSharp.SKManagedWStream.OnWrite(IntPtr buffer, IntPtr size)
   at SkiaSharp.SKAbstractManagedWStream.WriteInternal(IntPtr s, Void* context, Void* buffer, IntPtr size)
   at SkiaSharp.SkiaApi.sk_jpegencoder_encode(IntPtr dst, IntPtr src, SKJpegEncoderOptions* options)
   at SkiaSharp.SKPixmap.Encode(SKWStream dst, SKJpegEncoderOptions options)

See also: https://github.com/dotnet/runtime/issues/27117

Note that I'm also using SixLabors.ImageSharp in this project, and I'm using System.Numerics from .NET 4.8, but that's about it.

Code

This was triggered when saving a pixmap as jpeg or png.

I don't have a standalone code yet, working on that.

Expected Behavior

The pixmap is saved

Actual Behavior

System.IO.FileLoadException is thrown

Basic Information

  • Version with issue: 1.68.3
  • Last known good version: unknown
  • IDE: Visual Studio 2019
  • Platform Target Frameworks: Windows 10 2004

Workaround

For some reason assembly binding redirects didn't work for me, I added the following code in a static constructor as a temporary workaround:

			AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
			{
				switch (args.Name)
				{
					case "System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51":
						return typeof(System.Buffers.ArrayPool<>).Assembly;
					default:
						return null;
				}
			};

ziriax avatar Jun 05 '20 13:06 ziriax

What happen if you install the System.Buffers package: https://www.nuget.org/packages/System.Buffers/

Maybe that will help fix things...

What happens (or do you have) these packages installed:

  • System.IO.UnmanagedMemoryStream
  • System.Memory
  • System.Buffers

mattleibow avatar Jun 05 '20 14:06 mattleibow

Thanks for quick feedback.

What happen if you install the System.Buffers package: https://www.nuget.org/packages/System.Buffers/

Unfortunately, that doesn't help, same error.

What happens (or do you have) these packages installed:

  • System.IO.UnmanagedMemoryStream
  • System.Memory
  • System.Buffers

I have none of these installed, but since Sixlabors.ImageSharp references System.Memory and System.Buffers

Notice that the System.Buffers assembly in my build directory has assembly version 4.0.3.0, so it is very strange that this minor revision is not picked up automatically...

ziriax avatar Jun 05 '20 14:06 ziriax

similarly am facing issue with the System.Memory dll as below Could not load file or assembly 'System.Memory, Version=4.0.1.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 StreamJsonRpc.JsonMessageFormatter..ctor(Encoding encoding) at StreamJsonRpc.JsonRpc..ctor(Stream sendingStream, Stream receivingStream, Object target) at StreamJsonRpc.JsonRpc.Attach(Stream sendingStream, Stream receivingStream, Object target) at StreamJsonRpc.JsonRpc.Attach(Stream stream, Object target)

snr745 avatar Jul 06 '20 07:07 snr745

I am running into the same issue/error. The bindingRedirect in the App.config file is not respected by SkiaSharp. When I package my project into an installer and install locally, the app gives this error:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 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) ---> System.BadImageFormatException: Cannot load a reference assembly for execution. --- End of inner exception stack trace --- at SkiaSharp.Utils.RentedArray`1..ctor(Int32 length) at SkiaSharp.SKManagedStream.OnReadManagedStream(IntPtr buffer, IntPtr size) at SkiaSharp.SKManagedStream.OnPeek(IntPtr buffer, IntPtr size)

I've been pulling my hair out trying to figure out what I'm doing wrong. But it appears Skia is not respecting these my bindingRedirect correctly for some reason. No version of System.Buffers from NuGet resolves this error, and the error only occurs in an installed version of the app. In visual studio it runs fine.

I need to figure out how to duplicate what @ziriax did in his static constructor as a workaround in the mean time as I am currently hard stuck on the issue.

Edit: My issue turned out to be a corrupted dll in my NuGet cache being cached in a bunch of different locations. I had to remove the NuGet package from my project, then delete all bin folders and all the output folders for the installer project as well. Then remove the NuGet Cache from my user folder. Then re-add buffers 4.5.1 to my project. It finally worked at that point.

erebuswolf avatar May 29 '23 00:05 erebuswolf