System.Drawing.Common在docker报错 The type initializer for 'Gdip' threw an exception

今天在asp.net core站点上做一个发送邮件附带二维码的功能,为了方便邮件接受者直接手机扫描打开特定h5页面。采用QRCoder,代码很简单几行

 QRCodeGenerator qrGenerator = new QRCodeGenerator();
 QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
 Base64QRCode qrCode = new Base64QRCode(qrCodeData);

 return qrCode.GetGraphic(5);

返回二维码的base64格式,嵌入img中显示,运行->测试,一切顺利。 提交代码,生成镜像更新到站点,同样的路径走一遍,发现收到的邮件中没有图片。检查日志发现

An unhandled exception has occurred while executing the request.
System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory

at Interop.Libdl.dlopen(String fileName, Int32 flag)
at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary()

github早有前人提交过同样的问题,解决方法是在生成镜像时安装缺失的包


FROM microsoft/aspnetcore:2.0 AS base
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

重新生成测试通过。
因为安装包的缘故,整个镜像生成时间将近20分钟,我就顺便做了一个包含安装包的基础镜像,这样方便后续更新

docker push wyxnj/aspnetcore:latest

用这个作为asp.net core runtime的基础镜像,可以和以前一样快速生成镜像并更新站点。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章