After I upgraded to .Net Core 6, getting error- The type initializer for ' ' threw an exception

Hello Team,
I have an Azure Pipeline with my .Net Core 6.0 WebAPI project which is referenced the Nuget-Package Aspose.Words version 21.10.0, on my local win 10 OS it works fine. When we push our code to Higher environments starting with Dev, which is currently hosted using Azure in alpine linux throws an error,

2022-09-26 07:49:55 System.TypeInitializationException: The type initializer for '  ' threw an exception. 2022-09-26 07:49:55	
 ---> System.TypeInitializationException: The type initializer for '  ' threw an exception.
2022-09-26 07:49:55	
 ---> System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
2022-09-26 07:49:55	
en-nz is an invalid culture identifier.
2022-09-26 07:49:55	
   at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
2022-09-26 07:49:55	
   at   ..cctor()
2022-09-26 07:49:55	
   --- End of inner exception stack trace ---
2022-09-26 07:49:55	
   at   . (String )
2022-09-26 07:49:55	
   at   ..cctor()
2022-09-26 07:49:55	
   --- End of inner exception stack trace ---
2022-09-26 07:49:55	
   at   . (Double , Int32 )
2022-09-26 07:49:55	
   at   . (Double )
2022-09-26 07:49:55	
   at    .        (StringBuilder )
2022-09-26 07:49:55	
   at    .       (StringBuilder )
2022-09-26 07:49:55	
   at    .       (StringBuilder )
2022-09-26 07:49:55	
   at ​  .(StringBuilder )
2022-09-26 07:49:55	
   at    . ()
2022-09-26 07:49:55	
   at  ​ . ​    ()
2022-09-26 07:49:55	
   at  ​ . ​    ( ​  )
2022-09-26 07:49:55	
   at  ​ .( ​  )
2022-09-26 07:49:55	
   at  ​ . ​    ( ​  )
2022-09-26 07:49:55	
   at  ​ . ​    ( ​  )
2022-09-26 07:49:55	
   at  ​ . ()
2022-09-26 07:49:55	
   at  ​ . ()
2022-09-26 07:49:55	
   at  ​ . (Node )
2022-09-26 07:49:55	
   at  ​ .      (Node )
2022-09-26 07:49:55	
   at Aspose.Words.Node.ToString(SaveFormat saveFormat)

I could able to print the word document before upgrading to .Net Core 6 it was all working fine, but after .Net Core 6 upgrade changes we are getting the above error.

Below is the code where the error is pointing out

Document cloneAnswerTemplate = answerTemp.Clone();
Document answerDoc1 = new Document();
answerDoc1 = BuildAnswerDoc(answer, cloneAnswerTemplate);
answerDoc1.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
answerDoc1.LastSection.PageSetup.SectionStart = SectionStart.Continuous;
answerDoc.AppendDocument(answerDoc1, ImportFormatMode.KeepSourceFormatting);
answerDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
answerDoc.LastSection.PageSetup.SectionStart = SectionStart.Continuous;
answerDoc.LastSection.PageSetup.BottomMargin = 10.00;
                        
string answerhtml = answerDoc.Document.ToString(SaveFormat.Html);
ReplaceString("{{QuestionAnswers}}", answerhtml, cloneQuestionTemplate, true);

If I try to set ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false; it still does not work, and it gives a another error message. suggested link here
We are using - alpine linux . I have attached the docker file image as reference as well as complete error screenshot.
DockerFile.png (52.1 KB)
ErrorScreenshot.png (164.0 KB)

@sudi065 It looks like you have the same problem as described here:
https://docs.microsoft.com/en-us/answers/questions/728280/running-net-6-project-in-docker-throws-globalizati.html
To resolve the problem you need to install the following packages:

# Requred to make code works on 6.0-alpine3.15. See https://docs.microsoft.com/en-us/answers/questions/728280/running-net-6-project-in-docker-throws-globalizati.html
RUN apk add --no-cache icu-libs krb5-libs libgcc libintl libssl1.1 libstdc++ zlib

Here is my working Dokerfile for Alpine Linux:

FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine3.15 AS base
WORKDIR /app

# Required to make SkiaSharp 2.80.1 work. If use 2.80.3 SkiaSharp.NativeAssets.Linux.NoDependencies it is not required.
#RUN apk add --no-cache fontconfig
# Requred to make code works on 6.0-alpine3.15. See https://docs.microsoft.com/en-us/answers/questions/728280/running-net-6-project-in-docker-throws-globalizati.html
RUN apk add --no-cache icu-libs krb5-libs libgcc libintl libssl1.1 libstdc++ zlib
# For debug purposes.
RUN apk add --no-cache bash
# Add the missed ld-linux-x86-64.so.2 dependency.
RUN apk add --no-cache gcompat

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

FROM build AS publish
RUN dotnet publish "TestNet6.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", "TestNet6.dll"]

Also, please note that Aspose.Words for .NET6 or .NET Standard uses SkiaShap to deal with graphics. To make it work on Linux you should install SkiaSharp Linux native assets. Here is dependencies:

<PackageReference Include="Aspose.Words" Version="22.7.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.80.3" />

Note, I have used 2.80.3 version of SkiaSharp.NativeAssets because there was a mistake in NoDependencies package in 2.80.1 and it still requires fontconfig package (See comments in Dockerfile).

@alexey.noskov - I tried and still I got different error. Please find my attached docker file and error in details screenshot.
Process terminated. Couldn't find a valid ICU package installed on the system. Please install libicu using your package manager and try again. Alternatively you can set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support. Please see https://aka.ms/dotnet-missing-libicu for more information.

 <PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.80.3" />*

The reason I’m seeing this issue only where our API’s have used Aspose.Words and migrated to .Net 6.0 version, other APIs where we have upgraded did not see any of these issues which we are not utilizing Aspose.
Added in .csproj file as suggested [here](Running .NET 6 project in Docker throws Globalization.CultureNotFoundException - Microsoft Q&A 1)
InvariantGlobalization false

DockerFile.png (56.1 KB)
ErrorScreenshot.png (241.2 KB)

@sudi065 It looks like you installed the ICU packages in the build image, but the packages are required in the runtime image. In the Dockerfile I have provided the ICU packages are installed in base image, which is later used as a final.
Please try installing the ICU packages in the runtime image.

Thank you, after adding these 2 changes have resolved my issue.

adding below 2 in Docker file in image builder

RUN apk add --no-cache icu-libs krb5-libs libgcc libintl libssl1.1 libstdc++ zlib
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

and in .csproj file

<PropertyGroup>
     <InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>

@sudi065 It is perfect that you managed to resolve the problem on your side. Please feel free to ask in case of any issues. We are always glad to help you.

@alexey.noskov - Thank you very much for the support and appreciate all the efforts have been put

1 Like