The type initializer for 'SkiaSharp.SKObject' threw an exception

I am facing the following error while using Aspose.Words for .NET and Aspose.Cells for .NET (latest versions of both). The issue happens only when web app is deployed Linux based environment. I am using Docker to deploy the app. On Windows based development machine, I don’t face any issue:

Error:

Unhandled exception. System.TypeInitializationException: The type initializer for 'SkiaSharp.SKObject' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.6/libSkiaSharp.so: cannot open shared object file: No such file or directory
/app/libSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.6/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/app/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.6/libSkiaSharp: cannot open shared object file: No such file or directory
/app/libSkiaSharp: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.6/liblibSkiaSharp: cannot open shared object file: No such file or directory
/app/liblibSkiaSharp: cannot open shared object file: No such file or directory
   at SkiaSharp.SkiaApi.sk_version_get_milestone()
   at SkiaSharp.SkiaSharpVersion.get_Native()
   at SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(Boolean throwIfIncompatible)
   at SkiaSharp.SKObject..cctor()
   --- End of inner exception stack trace ---
   at SkiaSharp.SKObject.DeregisterHandle(IntPtr handle, SKObject instance)
   at SkiaSharp.SKObject.set_Handle(IntPtr value)
   at SkiaSharp.SKNativeObject.Dispose(Boolean disposing)
   at SkiaSharp.SKObject.Dispose(Boolean disposing)
   at SkiaSharp.SKBitmap.Dispose(Boolean disposing)
   at SkiaSharp.SKNativeObject.Finalize()

Code:

// Load the Word document
Document doc = new Document(memStream);

// Apply page limit
int numberOfPages = doc.PageCount < Constants.MAX_NUMBER_OF_PAGES_ALLOWED ? doc.PageCount : Constants.MAX_NUMBER_OF_PAGES_ALLOWED;

string extractedText = "";

Document extractedPages = doc.ExtractPages(0, numberOfPages);
extractedText = extractedPages.ToString(Aspose.Words.SaveFormat.Text);

// Create document for translated text
Document translatedDocument = new Document();
DocumentBuilder builder = new DocumentBuilder(translatedDocument);

// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.Alignment = ParagraphAlignment.Justify;
paragraphFormat.KeepTogether = true;

// Translate text
var operationResult = // Some processing on text.

if (operationResult.IsSuccess)
{
    if (operationResult.Message != "0")
    {
        builder.Writeln(operationResult.Message);
    }
}


// Updated file's path
string downloadFilePath = Path.Combine(Constants.DOWNLOADS_FOLDER, fileName);

// Save file
translatedDocument.Save(downloadFilePath);

//---------------------- Convert to Excel --------------------------- 
// Initialize Text File's Load options
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Csv);
// Load file
Workbook oExcelWorkBook = new Workbook(downloadFilePath, loadOptions);
// Converted file's path
string excelFilePath = Path.Combine(Constants.DOWNLOADS_FOLDER, Path.GetFileNameWithoutExtension(fileName) + fileExtension);
oExcelWorkBook.Save(excelFilePath);

Looking forward to your prompt response.

Thanks.

@uax99 Please check the following thread with the same issue .NET 8, Docker, Linux SkiaSharp Failing. Hope it will be useful for you.

1 Like

@uax99,

Same is the case with Aspose.Cells for .NET. Aspose.Cells for .NET uses SkiaSharp as a graphics library dependency on non-windows platforms (e.g., Linux system). See the document for your reference.
https://docs.aspose.com/cells/net/how-to-run-aspose-cells-for-net6/#run-asposecells-for-net6-on-linux

1 Like

@amjad.sahi,

Is the solution provided in documentation valid for .NET 8 also?

@uax99,

Yes, it should work fine for .NET 8 as well.