.NET 6 System.Drawing.Common 运行时开关

Kev*_*lef 15 system.drawing.common .net-6.0

根据https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only System.Drawing.Common 不再受非支持Windows 操作系统,除非设置了运行时配置开关。我已经设置了runtimeconfig.template.json并看到了开关:

"runtimeOptions": {
      "configProperties": {
        "System.Drawing.EnableUnixSupport": true
      }
    }
Run Code Online (Sandbox Code Playgroud)

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

但是,当我在 Linux 盒子中运行该应用程序时,dotnet exec app.dll我仍然得到 PlatformNotSupportedException

小智 24

以下内容对我有用。

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

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Run Code Online (Sandbox Code Playgroud)

接下来在项目文件所在的同一目录中创建一个名为 runtimeconfig.template.json 的文件,其中包含:

{
      "configProperties": {
         "System.Drawing.EnableUnixSupport": true
      }
}
Run Code Online (Sandbox Code Playgroud)

我使用了该dotnet publish命令,该命令在我提供给该命令的输出目录中创建了一个 [YourAppNameHere].runtimeconfig.json 文件dotnet publish

对于我的 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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但尝试遵循问题中链接的页面上的文档却没有。runtimeOptions我认为这是因为我在 runtimeconfig.template.json 文件中添加了该部分,但该dotnet publish命令还添加了一个名为 的部分runtimeOptions,这似乎阻止了运行时查看该System.Drawing.EnableUnixSupport选项。

因此,我排除了runTimeOptionsruntimeconfig.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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意嵌套runtimeOptions,我认为在尝试遵循问题链接中的文档时,它会导致它失败。

  • 这太糟糕了,我从 DNX 时代起就一直在使用 .net core,每次有新的更新时,他们都会做出一些糟糕的破坏性更改,需要对 csproj 和其他东西进行糟糕的添加才能使工作再次正常工作,这绝对令人沮丧。而这也是完全没有必要的。 (3认同)