Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function gives error: System.Drawing is not supported on this platform

(If this question is poorly worded, could someone please help me clear it up?)

I have an Azure Function (2.0) which relies on some System.Drawing code. I've added a NuGet reference to System.Drawing.Common (4.5.0).

After publishing the app, however, when the function is called, it produces the error:

System.Private.CoreLib: Exception while executing function: [MyFunctionName]. System.Drawing.Common: System.Drawing is not supported on this platform.

As far as I'm aware, System.Drawing.Common is supported on .NET Core now, which I believe is the environment in which my Azure Function is running. The actual project is a .NET Standard 2.0 project, though.

I am confused as to how to resolve this. I've tried converting the project to a .NET Core 2.1 project but that led to bizarre errors related to "Metadata generation failed" and an inability to find System.Runtime.

My project references Microsoft.Azure.WebJobs.Extensions.EventGrid (2.0.0-beta2) if that's relevant.

like image 397
vargonian Avatar asked Aug 17 '18 23:08

vargonian


1 Answers

It's not about the CLR, it's about the sandbox.

System.Drawing relies heavily on GDI/GDI+ to do its thing. Because of the somewhat risky nature of those APIs (large attack surface) they are restricted in the App Service sandbox.

Win32k.sys (User32/GDI32) Restrictions

For the sake of radical attack surface area reduction, the sandbox prevents almost all of the Win32k.sys APIs from being called, which practically means that most of User32/GDI32 system calls are blocked. For most applications this is not an issue since most Azure Web Apps do not require access to Windows UI functionality (they are web applications after all).

Try to find an different library that doesn't rely on System.Drawing/GDI, like ImageSharp.

like image 83
evilSnobu Avatar answered Sep 28 '22 12:09

evilSnobu