Skip to content

System.Globalization.CultureNotFoundException UnitsNet.ToString() on system that Culture information is not available #1238

Closed
@PRIMETSS

Description

@PRIMETSS

When .ToString() is called via DotNet #7 App on a UnitsNet Type on a system that either does not have Culture support with the
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='1' environment variable set (required to run dotnet on PI) in Culture Invariant mode.

Unhandled exception. System.TypeInitializationException: The type initializer for 'UnitsNet.UnitAbbreviationsCache' 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 UnitsNet.UnitAbbreviationsCache..cctor() --- End of inner exception stack trace --- at UnitsNet.UnitAbbreviationsCache.get_Default() at UnitsNet.UnitFormatter.GetFormatArgs[TUnitType](TUnitType unit, Double value, IFormatProvider culture, IEnumerable1 args)
at UnitsNet.QuantityFormatter.ToStringWithSignificantDigitsAfterRadix[TUnitType](IQuantity1 quantity, IFormatProvider formatProvider, Int32 number) at UnitsNet.QuantityFormatter.Format[TUnitType](IQuantity1 quantity, String format, IFormatProvider formatProvider)
at UnitsNet.Temperature.ToString(String format, IFormatProvider provider)
at UnitsNet.Temperature.ToString(String format)
at UnitsNet.Temperature.ToString()`

On OpenWRT SNAPShot (r22514) (Busybox Linux-musl ARM64) running on a Raspberry PI CM4 install latest DotNet SDK Version: 7.0.202 an ' System.Globalization.CultureNotFoundException' is thrown.

Expected behavior

string temp = sensorAht20.GetTemperature().ToString("F2")

Expected to return a 2 decimal places Temperature, eg '22.2' on system where Culture is not available.

image

https://learn.microsoft.com/en-us/dotnet/core/runtime-config/globalization

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions