Search code examples
.net-6.0system.drawing.common

.NET 6 System.Drawing.Common Runtime Switch


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


Solution

  • 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.