.NET 6 System.Drawing.Common Runtime Switch

14.1k views Asked by At

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

3

There are 3 answers

4
Dpc On BEST ANSWER

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.

1
tsul On

I finally got it working on Linux by downgrading System.Drawing.Common to 6.0.0
The answers listed above do not work for System.Drawing.Common 7.0.0 and above on .NET 6.

1
antonpv On

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

AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);