mattjohnsonpint / TimeZoneNames

Provides a complete source of localized time zone names and abbreviations.
MIT License
199 stars 32 forks source link

Windows Server 2016 - The type initializer for 'TimeZoneNames.TZNames' threw an exception. #65

Open akirby opened 3 years ago

akirby commented 3 years ago

I have the following implementation in .NET Framework 4.7.1

var tzNames = TZNames.GetAbbreviationsForTimeZone(tz.Id, CultureInfo.CurrentCulture.Name);

which throws the following error with stack trace: The type initializer for 'TimeZoneNames.TZNames' threw an exception. at TimeZoneNames.TZNames.GetLanguageKey(String languageCode, Boolean forDisplayNames) at TimeZoneNames.TZNames.GetAbbreviationsForTimeZone(String timeZoneId, String languageCode)

I have run windows update to ensure that I am current, but not sure if i need to install some other dependency on the server.

Running Get-Culture on the box returns

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)

I have also run txutil to update the Timezone registry, but not sure if this is the correct step.

Please let me know if there is something else I can check on my server, as this code works on my dev machine.

akirby commented 3 years ago

tz.Id verified through logging is "Eastern Standard Time".

The tz object is from TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

mattjohnsonpint commented 3 years ago

Nothing else should need to be installed. Could you please do some debugging on the application itself to see if "en-US" is being passed for the language code?

I hit something similar a long time ago with #32, but that has been resolved.

Could you please provide the specific exception name and message? I only see the stack trace. Thanks.

(Also, you should probably be using the CurrentUICulture instead of CurrentCulture, though I doubt that is the cause of the exception.)

robin-salih-fluke commented 3 years ago

I had a similar issue, this test demonstrates it:

image

The call to TimeZoneInfo.FindSystemTimeZoneById adds another entry to the list of windows time zones, which then causes ToDictionary to throw an exception as it is the duplicate key (case differs).

akirby commented 3 years ago

@robin-salih-fluke that is interesting.

Wangota commented 2 years ago

@mattjohnsonpint

Any chance this is fixed? I am experiencing the same exact issue: See stacktrace below. lean forDisplayNames) at TimeZoneNames.TZNames.GetTimeZonesForCountry(String countryCode, String languageCode, DateTimeOffset threshold) at BusinessLogic.Utils.TimeZones.TimeZonesByCountry(String countryCode, String language) at Events.Controllers.EventController.CountryTimeZones(String countryID)

Exception Message: The type initializer for 'TimeZoneNames.TZNames' threw an exception."

Wangota commented 2 years ago

@akirby @robin-salih-fluke Have you resolved this issue?

mattjohnsonpint commented 2 years ago

I'll take another look. Thanks.