Dot-Net-6.0

.NET 6 System.Drawing.Common 執行時開關

  • December 17, 2021

根據https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only System.Drawing.Common 不再支持在非windows OS 除非設置了執行時配置開關。我已經設置了 runtimeconfig.template.json 並看到了開關:

"runtimeOptions": {
     "configProperties": {
       "System.Drawing.EnableUnixSupport": true
     }
   }

在 bin/Debug/net6.0 中的文件 .runtimeconfig.json 中

但是,當我在 linux 機器中執行應用程序時,dotnet exec app.dll我仍然得到 PlatformNotSupportedException

以下對我有用。

將以下行添加到 PropertyGroup 部分的 .csproj 文件中:

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>

接下來在與項目文件相同的目錄中創建一個名為 runtimeconfig.template.json 的文件,其中包含:

{
     "configProperties": {
        "System.Drawing.EnableUnixSupport": true
     }
}

我使用了 dotnet publish 命令,它創建了一個

$$ YourAppNameHere $$我提供給 dotnet publish 命令的輸出目錄中的 .runtimeconfig.json 文件。 對於我的 asp.net 項目,發布結果如下

$$ YourAppNameHere $$.runtimeconfig.json 文件:

{
 "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 文件中添加了“runtimeOptions”部分,但 dotnet publish 命令還添加了一個名為“runtimeOptions”的部分,這似乎阻止了執行時看到“System. Drawing.EnableUnixSupport”選項。

出於這個原因,我在 runtimeconfig.template.json 文件中排除了“runTimeOptions”部分,因為發布導致以下文件不起作用:

{
 "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”,我認為在嘗試遵循問題連結中的文件時會導致它失敗。

引用自:https://stackoverflow.com/questions/70310747