Search code examples
c#dockerskiasharp

"The type initializer for 'SkiaSharp.SKAbstractManagedWStream' threw an exception." occuring if QuestPDF library published in Docker container


I am trying to use the QuestPDF library with docker container. If i try to send a request via Postman, the following exception was occured:

"The type initializer for 'SkiaSharp.SKAbstractManagedWStream' threw an exception."

This is my current Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal as build
WORKDIR /src
RUN apt-get update && apt-get install libfontconfig1 -y
COPY . .

RUN dotnet restore "./DataExportService.csproj"
RUN dotnet publish "./DataExportService.csproj" -c Release -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal
WORKDIR /app
COPY --from=build /app ./
EXPOSE 5000

ENTRYPOINT ["dotnet", "DataExportService.dll"]

Also i tried to add all needed nuget packages. VS Solution Explorer

Is it possible to suppress this error and make the container work correctly? I have no idea. Thanks.


Solution

  • I managed to solve the problem myself. Below you can find the updated Dockerfile.

    FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
    WORKDIR /app
    RUN apt-get update && apt-get install -y ttf-mscorefonts-installer fontconfig libc6 libc6-dev libgtk2.0-0 libnss3 libatk-bridge2.0-0 libx11-xcb1 libxcb-dri3-0 libdrm-common libgbm1 libasound2 libappindicator3-1 libxrender1 libfontconfig1 libxshmfence1
    RUN chmod 777 .
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
    WORKDIR /src
    COPY ["DataExportService.csproj", "DataExportService/"]
    RUN dotnet restore "DataExportService/DataExportService.csproj"
    
    WORKDIR "/src/DataExportService"
    COPY . .
    RUN dotnet build "DataExportService.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "DataExportService.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "DataExportService.dll"]
    

    Additional info: Also you need to specify a supported font in the document

    page.DefaultTextStyle(x => x.FontFamily(Fonts.Arial));