根据https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only,除非设置了运行时配置开关,否则在非windows操作系统下不再支持System.Drawing.Common。我已经设置了runtimeconfig.template.json并看到了开关:
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
在bin/Debug/net6.0中的文件.runtimeconfig.json中
然而,当我使用dotnet exec app.dll
在linux盒中运行这个应用程序时,我仍然可以得到PlatformNotSupportedException。
发布于 2021-12-17 00:51:07
下面这句话对我很管用。
将以下行添加到.csproj文件中的PropertyGroup部分中:
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
接下来,在与项目文件相同的目录中创建一个名为runtimeconfig.template.json的文件,其中包含:
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
我使用了dotnet发布命令,它在我提供给dotnet发布命令的输出目录中创建了一个YourAppNameHere.runtimeconfig.json文件。
对于我的asp.net项目,发布导致了以下YourAppNameHere.runtimeconfig.jsonfile:
{
"runtimeOptions": {
"tfm": "net6.0",
"includedFrameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "6.0.1"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "6.0.1"
}
],
"configProperties": {
"System.Drawing.EnableUnixSupport": true,
"System.GC.Server": true,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
这样做是可行的,因为试图跟踪问题中链接到的页面上的文档并没有成功。我认为这是因为我在runtimeconfig.template.json文件中添加了“runtimeconfig.template.json”部分,但是dotnet发布命令也添加了一个名为"runtimeOptions“的部分,这似乎阻止了运行时看到"System.Drawing.EnableUnixSupport”选项。
由于这个原因,我在我的runTimeOptions文件中排除了“runtimeconfig.template.json”部分,因为发布导致以下文件无法工作:
{
"runtimeOptions": {
"tfm": "net6.0",
"includedFrameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "6.0.1"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "6.0.1"
}
],
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
},
"configProperties": {
"System.GC.Server": true,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
注意嵌套的"runtimeOptions",我认为这会导致它在试图跟踪问题链接中的文档时失败。
发布于 2022-11-16 20:34:58
另一种方法是在代码中使用set开关:
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
https://stackoverflow.com/questions/70310747
复制相似问题