Humanizer icon indicating copy to clipboard operation
Humanizer copied to clipboard

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

Open nssidhu opened this issue 3 years ago • 7 comments

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)

nssidhu avatar Sep 23 '21 23:09 nssidhu

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

clairernovotny avatar Sep 25 '21 13:09 clairernovotny

Yes I am in setting in project file as shown in the document.

Sent from Yahoo Mail for iPhone

On Saturday, September 25, 2021, 8:59 AM, Claire Novotny @.***> wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

nssidhu avatar Sep 25 '21 18:09 nssidhu

Hi, receive same error on console application net6.0

franklupo avatar Feb 24 '22 16:02 franklupo

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!

kylejuliandev avatar Mar 07 '22 17:03 kylejuliandev

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!

SuperJMN avatar Mar 17 '22 09:03 SuperJMN

Hi, receive same error on console application net6.0

Now work

franklupo avatar May 10 '22 12:05 franklupo

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

ptupitsyn avatar Jun 25 '22 15:06 ptupitsyn

Can anyone merge the PR that fixes this??

mikernet avatar Aug 07 '23 09:08 mikernet

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

sangeethnandakumar avatar Feb 15 '24 17:02 sangeethnandakumar

@sangeethnandakumar there is a PR. https://github.com/Humanizr/Humanizer/pull/1213

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

SimonCropp avatar Feb 15 '24 23:02 SimonCropp