将ASP.NET Core发布到CentOS 7的服务器上,验证码无法显示。查看日志,报如下错误:

Unhandled Exception: 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() at System.Drawing.SafeNativeMethods.Gdip..cctor() --- End of inner exception stack trace --- at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromDelegate_linux(StreamGetHeaderDelegate getHeader, StreamGetBytesDelegate getBytes, StreamPutBytesDelegate putBytes, StreamSeekDelegate doSeek, StreamCloseDelegate close, StreamSizeDelegate size, IntPtr& image) at System.Drawing.Image.InitFromStream(Stream stream) at System.Drawing.Image.LoadFromStream(Stream stream, Boolean keepAlive)

解决办法

使用下面的命令创建软连接

ln -s libdl-2.17.so libdl.so 

libdl-2.17.so 所在的目录为 /usr/lib64

上面的操作结束后,再次运行验证码还是不能显示,再次查看日志,这次报了另外一个错误

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libgdiplus': The specified module could not be found.

这个错误是因为没有安装libgdiplus,直接使用下面的命令安装即可。

yum install libgdiplus-devel 

再次运行程序,验证码正常显示