VahidN / iTextSharp.LGPLv2.Core

iTextSharp.LGPLv2.Core is an unofficial port of the last LGPL version of the iTextSharp (V4.1.6) to .NET Core
Other
615 stars 154 forks source link

System.Runtime.Loader.AssemblyLoadContext is not available in UWP projects #104

Closed dahovey closed 1 year ago

dahovey commented 2 years ago

Summary of the issue

With UWP project exception occurs when running BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED) in very simple test project:

System.TypeLoadException: Could not load type 'System.Runtime.Loader.AssemblyLoadContext' from assembly 'System.Runtime.Loader, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at iTextSharp.text.pdf.BaseFont.GetResourceStream(String key)
   at iTextSharp.text.pdf.Type1Font..ctor(String afmFile, String enc, Boolean emb, Byte[] ttfAfm, Byte[] pfb, Boolean forceRead)
   at iTextSharp.text.pdf.BaseFont.CreateFont(String name, String encoding, Boolean embedded, Boolean cached, Byte[] ttfAfm, Byte[] pfb, Boolean noThrow, Boolean forceRead)
   at iTextSharp.text.pdf.BaseFont.CreateFont(String name, String encoding, Boolean embedded)
   at ClassLibrary1.Class1.InitializeFont()

Environment

iTextSharp.LGPLv2.Core version: 1.5.0 thru 1.7.5
.NET Core SDK version: N/A
UWP Windows Build: 16299
IDE: Visual Studio 2019 16.11

Example code/Steps to reproduce:

If I create a blank UWP project, reference NuGet System.Runtime.Loader 4.3.0 and call AssemblyLoadContext.Default the above exception occurs.

Interestingly, I have been using version 1.4.5 successfully with UWP. I believe this is because of a try catch (that was later removed starting in version 1.5.0) . See here. After upgrading NuGet package to 1.5.0 an exception occurs because loadProperties within the CjkFont` class does not include try/catch block. Subsequently, when I call FontFactory.GetFont, even with my custom registered font I get the below exception:

System.TypeLoadException: Could not load type 'System.Runtime.Loader.AssemblyLoadContext' from assembly 'System.Runtime.Loader, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at iTextSharp.text.pdf.BaseFont.GetResourceStream(String key)
   at iTextSharp.text.pdf.CjkFont.loadProperties()
   at iTextSharp.text.pdf.CjkFont.IsCjkFont(String fontName, String enc)
   at iTextSharp.text.pdf.BaseFont.CreateFont(String name, String encoding, Boolean embedded, Boolean cached, Byte[] ttfAfm, Byte[] pfb, Boolean noThrow, Boolean forceRead)
   at iTextSharp.text.pdf.BaseFont.CreateFont(String name, String encoding, Boolean embedded)
   at SampleApp.ViewModels.DashboardViewModel.<get_OpenPdfCommand>b__26_0()
MestreDosMagros commented 1 year ago

Any news on this?

kurema commented 1 year ago

I have a similar issue. msbuild fails for BaseFont.GetResourceStream(string). This is not BaseFont.CreateFont() so maybe it's off-topic.

  C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): error : Type 'System.Runtime.Loader.AssemblyLoadContext' from assembly 'mscorlib' was not included in compilation, but was referenced in method 'BaseFont.GetResourceStream(string)'. There may have been a missing assembly, or a dependency on a more recent Windows SDK release. [D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\BookViewerApp.csproj]
  C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): error : Type 'System.Runtime.Loader.AssemblyLoadContext' from assembly 'mscorlib' was not included in compilation, but was referenced in method 'BaseFont.GetResourceStream(string)'. There may have been a missing assembly, or a dependency on a more recent Windows SDK release. [D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\BookViewerApp.csproj]
  C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): error : Method 'BaseFont.GetResourceStream(string)' will always throw an exception due to the missing method 'AssemblyLoadContext.get_Default()' in assembly 'System.Runtime.Loader'. There may have been a missing assembly, or a dependency on a more recent Windows SDK release. [D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\BookViewerApp.csproj]
  C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): error : Method 'BaseFont.GetResourceStream(string)' will always throw an exception due to the missing method 'AssemblyLoadContext.get_Default()' in assembly 'System.Runtime.Loader'. There may have been a missing assembly, or a dependency on a more recent Windows SDK release. [D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\BookViewerApp.csproj]
  C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\Microsoft.NetNative.targets(809,5): error : ILT0005: 'C:\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\runtime.win10-x86.microsoft.net.native.compiler\2.2.12-rel-31116-00\tools\x86\ilc\Tools64\nutc_driver.exe @"D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\obj\x86\Release\ilc\intermediate\MDIL\BookViewerApp.rsp"' returned exit code -1073740791 [D:\a\BookViewerApp3\BookViewerApp3\BookViewerApp\BookViewerApp\BookViewerApp.csproj]

In my case, v1.9.3 works fine. v3.1.0 do not.

But AssemblyLoadContext.Default is always in try{} (v3.3.0 / v1.9.3). I don't know why this happened.