14

According to https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only System.Drawing.Common is no longer supported under on non-windows OS UNLESS a runtime configuration switch is set. I've setup runtimeconfig.template.json and see the switch:

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

inside the file .runtimeconfig.json in bin/Debug/net6.0

However when I run the app in a linux box using dotnet exec app.dll I still get PlatformNotSupportedException

Kevin Micallef
  • 143
  • 1
  • 1
  • 4

2 Answers2

21

The following worked for me.

Adding the following line to the .csproj file in a PropertyGroup section:

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>

Next create a file named runtimeconfig.template.json in the same directory as your project file containing:

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

I used the dotnet publish command, which created a [YourAppNameHere].runtimeconfig.json file in the output directory I supplied to the dotnet publish command.

For my asp.net project, the publish resulted in the following [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
    }
  }
}

This worked, where trying to follow the documentation on the page linked to in the question did not. I think this is because I was adding the runtimeOptions section in the runtimeconfig.template.json file, but the dotnet publish command was also adding a section named runtimeOptions, which seems to have prevented the runtime from seeing the System.Drawing.EnableUnixSupport option.

For this reason, I excluded the runTimeOptions section in my runtimeconfig.template.json file as the publish resulted in the following file that did not work:

{
  "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
    }
  }
}

Note the nested runtimeOptions, which I believe was causing it to fail when trying to follow the documentation in the link from the question.

An Nguyen
  • 96
  • 1
  • 2
  • 6
Dpc
  • 326
  • 2
  • 4
  • 1
    The nested "runtimeOptions" was the issue. – Kevin Micallef Jan 03 '22 at 21:15
  • 3
    This sucks, I've been using .net core since the DNX era and everytime there's a new update they make some shitty breaking changes that require a shitty addition to csproj and other stuff to make what was working work again, this is absolutely frustrating. And this one is also absolutely unnecessary. – Felype Mar 08 '22 at 19:30
  • This works in .Net Core 3.1 as well. I had to update System.Drawing.Common to version 6 for another reason and wasn't sure if this would work since it doesn't have a `runtimeconfig.json` file, but if you create one it will work. – George May 09 '22 at 23:15
2

One more way is to use set switch in the code:

AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
antonpv
  • 807
  • 1
  • 10
  • 9