.NET Core|--SkiaSharp 生成图像报错|--The type initializer for 'SkiaSharp.SKAbstractManagedStream' threw an exception.

前言


#SkiaSharp - 版本
2.88.3

#SkiaSharp.NativeAssets.Linux.NoDependencies - 版本
2.88.3

#部署服务器环境 
Linux / docker / 

#Visual Studio 版本
Microsoft Visual Studio Professional 2022 (64 位)

#C#语言版本
10.0

首先我只是安装了SkiaSharp, 此时在本地Windows系统下生成图片是没有问题的,
但是部署到Linux的docker容器中, 就会报错了( 报错信息见下 )

报错信息

System.TypeInitializationException: The type initializer for 'SkiaSharp.SKAbstractManagedStream' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: 
/app/runtimes/linux-x64/native/libSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libSkiaSharp.so: cannot open shared object file: No such file or directory
/app/libSkiaSharp.so: cannot open shared object file: No such file or directory
/app/runtimes/linux-x64/native/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/app/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/app/runtimes/linux-x64/native/libSkiaSharp: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libSkiaSharp: cannot open shared object file: No such file or directory
/app/libSkiaSharp: cannot open shared object file: No such file or directory
/app/runtimes/linux-x64/native/liblibSkiaSharp: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/liblibSkiaSharp: cannot open shared object file: No such file or directory
/app/liblibSkiaSharp: cannot open shared object file: No such file or directory

解决方案

安装SkiaSharp.NativeAssets.Linux.NoDependencies即可

NuGet\Install-Package SkiaSharp.NativeAssets.Linux.NoDependencies -Version 2.88.3

解决问题途中的一些弯路

解决途中我好像同时安装了
SkiaSharp.NativeAssets.LinuxSkiaSharp.NativeAssets.Linux.NoDependencies,
结果还是会报错, 过犹不及了呗? 或者安装顺序不对? 或者是我本机的问题?

不过大家可以注意一下, 如果像我一样, 想求一个"双保险", 结果还是报错的话,
不妨卸载其中一个试试看.

其它参考解决方案( 我并没有使用该方式,仅作参考 )

→→→ 进入docker内部执行命令

#也可在docker中运行下面命令
apt-get update -y 
apt-get install -y libgdiplus 
apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

**→→→ 修改Dockerfile文件, 增加RUN命令 **

FROM microsoft/dotnet:2.2.0-aspnetcore-runtime
WORKDIR /app
COPY . .
RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
EXPOSE 80
ENTRYPOINT ["dotnet", "<你的入口程序集>"]

参考文档

posted @ 2023-07-28 18:18  zh89233  阅读(1269)  评论(5编辑  收藏  举报