System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode.

Raymond Tang Raymond Tang 0 4405 5.53 index 4/25/2023

When running ASP.NET Core application in Docker, you may hit the following error:

System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')

This article provides solutions for this issue.

Root cause

The image used doesn't support globalization while the ASP.NET Core application needs it. To address this issue, we need to use images that support globalization.

Internal Components for Unicode (ICU)

For many cases, we need ICU support and/or tzdata support. However, not all the published .NET Core images have these built-in. For example, only the following official published .NET container images that include ICU:

  • Alpine sdk images
  • Debian images
  • Ubuntu images

The following images do not include ICU:

  • Alpine aspnet, monitor, runtime, runtime-deps images
  • Ubuntu chiseled images

Resolutions

To resolve this issue, we can either pick up the image with ICU built-in or add the support directly. For example, if you are using mcr.microsoft.com/dotnet/aspnet:7.0.4-alpine3.16, we can change it to mcr.microsoft.com/dotnet/aspnet:7.0.5-bullseye-slim.  

Alternatively, we can add support into Alpine images:

# final stage/imageFROM mcr.microsoft.com/dotnet/aspnet:7.0.4-alpine3.16
ENV \    
DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \    
LC_ALL=en_US.UTF-8 \    
LANG=en_US.UTF-8
RUN apk add --no-cache \    
icu-data-full \    
icu-libs

References

Enabling (or disabling) globalization functionality

asp.net-core docker dotnetcore

Join the Discussion

View or add your thoughts below

Comments