How to resolve "The type initializer for 'Gdip' threw an exception" for using ClosedXML in dotnet core webapi in Kubernates deployement?

Elaya Raja 96 Reputation points
2022-06-09T17:07:06.653+00:00

I am using ClosedXml for my controller to return the excel file as response and it was working in windows local environment. I deployed the code in Kubernates and found the below exceptions in logs and not working. What was the solution to resolve this issue?

"Detail=The type initializer for 'Gdip' threw an exception.,
Exception=System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.\n --->
System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms.
See https://aka.ms/systemdrawingnonwindows for more information.\n
at System.Drawing.LibraryResolver.EnsureRegistered()\n
at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()\n
at System.Drawing.SafeNativeMethods.Gdip..cctor()\n --- End of inner exception stack trace ---\n
at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily)\n
at System.Drawing.FontFamily.GetGdipGenericSansSerif()\n
at System.Drawing.FontFamily.get_GenericSansSerif()\n
at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)\n
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)\n
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style)\n
at ClosedXML.Excel.FontBaseExtensions.GetCachedFont(IXLFontBase fontBase, Dictionary2 fontCache)\n at ClosedXML.Excel.FontBaseExtensions.GetWidth(IXLFontBase fontBase, String text, Dictionary2 fontCache)\n
at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow, Double minWidth, Double maxWidth)\n
at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow)\n
at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow)\n at ClosedXML.Excel.XLColumn.AdjustToContents()\n
at ClosedXML.Excel.XLColumns.<>c.<AdjustToContents>b__8_0(XLColumn c)\n
at System.Collections.Generic.List1.ForEach(Action1 action)\n
at ClosedXML.Excel.XLColumns.AdjustToContents()\n

Thanks
Jo

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,766 Reputation points
    2022-06-09T20:05:38.053+00:00

    the error is pretty clear. you must use a windows container, which requires a windows worker node or switch to a library that supports linux.

    windows containers in Kubernetes:

    https://kubernetes.io/docs/concepts/windows/intro/

    0 comments No comments

  2. Elaya Raja 96 Reputation points
    2022-06-10T01:32:47.303+00:00

    Thanks for the update. Is there any open source available for creating Excel in dotnet core webapi with kubernates support?

    Thanks in advance.
    Jo

    0 comments No comments