.Net Core 发布,在CentOS报错:System.PlatformNotSupportedException: System.Drawing.Common is not supported.

   错误原因  

       .net core mvc项目在windows中部署正常运行,但在Centos中部署Asp.Net Core 6.0 Mvc项目时报异常错误:System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.

具体错误见下图:

.Net Core 发布,在CentOS报错:System.PlatformNotSupportedException: System.Drawing.Common is not supported._第1张图片

解决方法

      vs2022在发布项目后,会在发布文件中生成一个[appname].runtimeconfig.json文件,只需要修改该配置文件即可。

例如:生成的 Angel.Web.runtimeconfig.json 文件 

.Net Core 发布,在CentOS报错:System.PlatformNotSupportedException: System.Drawing.Common is not supported._第2张图片

     我们可以看到:configProperties属性,并在下面添加: "System.Drawing.EnableUnixSupport": true 这样我们就可以在Linux中使用这个库了。

.Net Core 发布,在CentOS报错:System.PlatformNotSupportedException: System.Drawing.Common is not supported._第3张图片

其他异常错误

       System.Drawing.Common 出现 The type initializer for ‘Gdip’ threw an exception,

The type initializer for 'Gdip' threw an exception.

  Linux上需要安装 libgdiplus

yum search libgdiplus

yum install  libgdiplus-xxx.x86_64   这个版本号,可以在上面命令中查询到

如果yum中没有请更换一下yum源

等异常的解决办法。

libgdiplus是System.Drawing.Common原生端跨平台实现的主要提供者,是开源mono项目

       官方不再建议使用libgdiplus,主要是原因是libgdiplus非常臃肿、外部依赖众多、未解决bug及异常情况难以修复,修改[appname].runtimeconfig.json只是临时方案,需要逐步迁移到以下项目

官方参考资料:Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn

你可能感兴趣的:(asp.net,core,Linux操作系统,.netcore)