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

Hi,

I am running a project using Aspose Words for .NET using version 21.11.0. When I try to process a document with an image included in it, this error is being thrown.

System.TypeInitializationException: 'The type initializer for 'SkiaSharp.SKObject' threw an 
exception.'

DllNotFoundException: Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibSkiaSharp: cannot open shared object file: No such file or directory

This exception was originally thrown at this call stack:
SkiaSharp.SkiaSharpVersion.Native.get()
SkiaSharp.SkiaSharpVersion.CheckNativeLibraryCompatible(bool)
SkiaSharp.SKObject.SKObject()

My project is being run inside of a docker container using a debian based linux x64 architecture.

These are the related packages I have installed

  • Aspose words 21.11.0
  • SkiaSharp 2.80.3
  • SkiaSharp.NativeAssets.Linux 2.80.3
  • System.Drawing.Common 5.02

This is my Docker file

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y libfreetype6
RUN apt-get install -y libfontconfig1
RUN apt-get install -y libgdiplus
RUN apt-get install -y libc6-dev

COPY ["source/Host.DocumentReplace/Fonts/", "/usr/share/fonts/truetype/msfonts/"]

RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig

RUN apt-get install -y wget
RUN wget https://github.com/google/fonts/archive/main.tar.gz -O gf.tar.gz
RUN tar -xf gf.tar.gz
RUN mkdir -p /usr/share/fonts/truetype/google-fonts
RUN find $PWD/fonts-main/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1
RUN rm -f gf.tar.gz
RUN rm -rf /var/cache/*
RUN apt-get install fontconfig
RUN fc-cache -f

WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["source/Host.DocumentReplace/Host.Document.Api.csproj", "source/Host.DocumentReplace/"]
COPY ["source/Contracts.Service.Document/Contracts.Service.Document.csproj", "source/Contracts.Service.Document/"]
COPY ["source/Domain.Document/Domain.Document.csproj", "source/Domain.Document/"]
COPY ["source/Utility.Document/Utility.Document.csproj", "source/Utility.Document/"]
COPY ["source/Contracts.Manager.Document/Contracts.Manager.Document.csproj", "source/Contracts.Manager.Document/"]
COPY ["source/Contracts.Resources.Document/Contracts.Resources.Document.csproj", "source/Contracts.Resources.Document/"]
COPY ["source/Services.Document/Services.Document.csproj", "source/Services.Document/"]
COPY ["Handler.Document/Handler.Document.csproj", "Handler.Document/"]
COPY ["Manager.Document/Manager.Document.csproj", "Manager.Document/"]
RUN dotnet restore "source/Host.DocumentReplace/Host.Document.Api.csproj"
COPY . .
WORKDIR "/src/source/Host.DocumentReplace"
RUN dotnet build "Host.Document.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Host.Document.Api.csproj" \
            --configuration Release \ 
            --runtime linux-x64 \
            --self-contained false \ 
            --output /app/publish \
            -p:PublishReadyToRun=true 

FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "bootstrap.dll"]

Any help on this would be greatly appreciated

@dan457 First of all I would suggest you to use SkiaSharp and SkiaSharp.NativeAssets.Linux 2.80.2 instead of 2.80.3. There were several issues with 2.80.3 when it crashes while processing some images.
Second you can try using SkiaSharp.NativeAssets.Linux.NoDependencies

I tried to reproduce the issue on my side but did not manage to do this. I used the following simple project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Aspose.Words" Version="21.11.0" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux " Version="2.80.3" />
  </ItemGroup>

</Project>

C# code:

using Aspose.Words;
using System;

namespace Aspose.NetCore.TestRunner
{
    class Program
    {
        static void Main(string[] args)
        {
            License lic = new License();
            lic.SetLicense("/temp/Aspose.Words.NET.lic");

            Console.WriteLine("Test started.");

            Document document = new Document("/temp/in.docx");
            document.Save(@"/temp/out.pdf");

            Console.WriteLine("Done");
        }
    }
}

Dockerfile:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/runtime:3.1-buster-slim AS base
WORKDIR /app
RUN apt-get update && apt-get install -y libfontconfig1

FROM mcr.microsoft.com/dotnet/sdk:3.1-buster AS build
WORKDIR /src
COPY ["TestNet.csproj", "."]
RUN dotnet restore "./TestNet.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TestNet.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestNet.csproj" -c Release -r linux-x64 --no-self-contained -o /app/publish

FROM base AS final

WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestNet.dll"]

Build and run it using the following commands:

docker build -t awbug .
docker run --mount type=bind,source=C:\Temp,target=/temp --rm awbug from Docker

Document with image is converted fine on my side. I doubt that the problem is in your document, but just in case, please attach it here for testing.