Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The type initializer for 'Humanizer.Configuration.Configurator' threw an exception. #1126

Closed
nssidhu opened this issue Sep 23, 2021 · 10 comments · Fixed by #1468
Closed

The type initializer for 'Humanizer.Configuration.Configurator' threw an exception. #1126

nssidhu opened this issue Sep 23, 2021 · 10 comments · Fixed by #1468

Comments

@nssidhu
Copy link

nssidhu commented Sep 23, 2021

I am using the Humanizer.core in .NET Core 6 RC1 in Blazor WASM App.

This is what is throwing error in .razor page.
@searchResult.AlerSentTime?.Humanize(culture:CultureInfo.InvariantCulture);

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The type initializer for 'Humanizer.Configuration.Configurator' threw an exception.
System.TypeInitializationException: The type initializer for 'Humanizer.Configuration.Configurator' threw an exception.
 ---> System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
en-US is an invalid culture identifier.
   at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride)
   at System.Globalization.CultureInfo..ctor(String name)
   at Humanizer.Localisation.Formatters.DefaultFormatter..ctor(String localeCode) in /_/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs:line 19
   at Humanizer.Configuration.FormatterRegistry..ctor() in /_/src/Humanizer/Configuration/FormatterRegistry.cs:line 9
   at Humanizer.Configuration.Configurator..cctor() in /_/src/Humanizer/Configuration/Configurator.cs:line 28
   --- End of inner exception stack trace ---
   at Humanizer.DateHumanizeExtensions.Humanize(DateTime input, Nullable`1 utcDate, Nullable`1 dateToCompareAgainst, CultureInfo culture) in /_/src/Humanizer/DateHumanizeExtensions.cs:line 26
   at Humanizer.DateHumanizeExtensions.Humanize(Nullable`1 input, Nullable`1 utcDate, Nullable`1 dateToCompareAgainst, CultureInfo culture) in /_/src/Humanizer/DateHumanizeExtensions.cs:line 41
   at GetInLineV6New.Client.Pages.Org.OperateOrgLocationTerminal.<>c__DisplayClass31_0.<BuildRenderTree>b__16(RenderTreeBuilder __builder3) in c:\Users\admin\source\repos\XXXXXV6New\XXXXV6New\Client\Pages\Org\OperateOrgLocationTerminal.razor:line 92
   at Microsoft.AspNetCore.Components.Web.Virtualization.Virtualize`1[[XXXXXV6New.Shared.Models.DTO_Token, GetInLineV6New.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].BuildRenderTree(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
@clairernovotny
Copy link
Member

Are you setting the global invariant mode anywhere as per https://aka.ms/GlobalizationInvariantMode?

@nssidhu
Copy link
Author

nssidhu commented Sep 25, 2021 via email

@franklupo
Copy link

Hi,
receive same error on console application net6.0

@kylejuliandev
Copy link

kylejuliandev commented Mar 7, 2022

Hi,

I got the above error after migrating my NET 5 application to NET 6. I was not explicitly setting the Globalization configuration setting in the runtimeconfig.json or Project file. Upon further reading I discovered this might have something to do with the following breaking change, Culture creation and case mapping in globalization-invariant mode. I don't happen to experience this issue when developing locally, however, once I deploy using the Alpine docker image. The documentation does explicitly mention

Globalization-invariant mode is enabled by default on some Docker containers, for example, Alpine containers.

In my code, I create a custom type that implements ICollectionFormatter. I was adding this to the Configurator CollectionFormatters, as such.

Humanizer.Configuration.Configurator.CollectionFormatters.Register("en", new CustomFormatter("and"));

The fix was to disable InvariantGlobalization in the Project file, by setting InvariantGlobalization to false. Additionally, in my Dockerfile, shortly after I pull the Alpine image, I needed to install the ICU packages.

ARG AspNetVersion
FROM mcr.microsoft.com/dotnet/aspnet:${AspNetVersion} AS base
WORKDIR /app

...

RUN apk add --no-cache icu-libs

I found this article by Andrew Lock to be help!

I hope this helps!

@SuperJMN
Copy link

I having the same issue. .NET 6 application using GlobalizationInvariantMode.

Is there any way to make this library work in such applications? In my case, it's not in my hands to disable GlobalizationInvariantMode. Thanks!

@franklupo
Copy link

Hi, receive same error on console application net6.0

Now work

@ptupitsyn
Copy link

Same problem here, and I don't want to disable InvariantGlobalization.

@mikernet
Copy link

mikernet commented Aug 7, 2023

Can anyone merge the PR that fixes this??

@sangeethnandakumar
Copy link

The same issue happened today .NET 8 Docker (Alpine). Is there a fix guys?

@SimonCropp
Copy link
Collaborator

@sangeethnandakumar there is a PR. #1213

but, before i merge it, i need some tests to confirm it fixes the actual issue

bramvandenbussche added a commit to bramvandenbussche/readwiser that referenced this issue Jun 16, 2024
Fixed the issue of the Humanize constructor crashing when
in a docker container using this issue:
Humanizr/Humanizer#1126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants