weixin_39526564
weixin_39526564
2021-01-11 10:49

CultureNotFoundException in System.Globalization.CultureData.GetCultureData

Description

I'm not sure whether to post this in the wpf repo or in the runtime repo, but since the exception appears to happen in System.Globalization I'll try posting it here.

My WPF application started out as a dotnet core 3.1 app. Upgrading to .NET 5 Preview 2 went smoothly, so did the upgrade to Preview 3, but neither Preview 4 nor 5 work properly due to some kind of change in how cultures are handled. I can start the app without issues, but as soon as I click on, for example, a TextBox control it crashes with the following message: System.Globalization.CultureNotFoundException: 'Culture is not supported.'

My application contains no code to handle or set cultures manually.

I've put together a public repo that reproduces the issue. All I've done is create a new WPF app, replacing the default Grid with a DockPanel containing a TextBlock and an empty TextBox. As soon as I click somewhere in the TextBox, the application crashes.

The repo is here: https://github.com/bjorhn/culture-not-found

Configuration

Windows settings: display language regional settings Windows 10 Pro x64 (Build 18363)

Visual Studio/.NET: .NET 5 Preview 4 or 5 x64 Visual Studio 16.7 Preview 2

Regression

Everything worked as expected before .NET 5 Preview 4 (tested in dotnet 3.1, .NET 5 Preview 2 and .NET 5 Preview 3).

Other information

I have found no workarounds. I ran the following code snippet which I grabbed from a possibly related issue (https://github.com/dotnet/runtime/issues/37146):


Console.WriteLine("CurrentCulture      : " + System.Globalization.CultureInfo.CurrentCulture);
Console.WriteLine("CurrentCulture   (F): " + System.Globalization.CultureInfo.CurrentCulture.GetConsoleFallbackUICulture());
Console.WriteLine("CurrentUICulture    : " + System.Globalization.CultureInfo.CurrentUICulture);
Console.WriteLine("CurrentUICulture (F): " + System.Globalization.CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture());

These are the results:


net 5.0 preview 3 (no crash):
"CurrentCulture      : en-SE"
"CurrentCulture   (F): en-US"
"CurrentUICulture    : en-US"
"CurrentUICulture (F): en-US"`

net 5.0 preview 4 (crash when clicking on textbox: System.Globalization.CultureNotFoundException: 'Culture is not supported.'):
"CurrentCulture      : en-SE"
"CurrentCulture   (F): en-US"
"CurrentUICulture    : en-SE"
"CurrentUICulture (F): en-US"

net 5.0 preview 5 (crash when clicking on textbox: System.Globalization.CultureNotFoundException: 'Culture is not supported.'):
"CurrentCulture      : en-SE"
"CurrentCulture   (F): en-US"
"CurrentUICulture    : en-US"
"CurrentUICulture (F): en-US"

As you can see, there is a change in CurrentUICulture in Preview 4, but Previews 3 and 5 produce identical outputs. One really odd thing is that even after changing the region (Country) to United States and the Regional format to English (United States), I still get the same exception.

Exceptions

Preview 4:


System.Globalization.CultureNotFoundException
  HResult=0x80070057
  Message=Culture is not supported.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture)
   at System.Windows.Input.InputLanguageSource.get_CurrentInputLanguage()
   at System.Windows.Input.InputLanguageManager.get_CurrentInputLanguage()
   at System.Windows.Documents.TextSelection.EnsureCaret(Boolean isBlinkEnabled, Boolean isSelectionActive, CaretScrollMethod scrollMethod)
   at System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight()
   at System.Windows.Documents.TextEditor.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
   at System.Windows.Controls.Primitives.TextBoxBase.OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
   at System.Windows.UIElement.OnGotKeyboardFocusThunk(Object sender, KeyboardFocusChangedEventArgs e)
   at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
   at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
   at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
   at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
   at System.Windows.UIElement.Focus()
   at System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(TextEditor This)
   at System.Windows.Documents.TextEditorMouse.OnMouseDown(Object sender, MouseButtonEventArgs e)
   at System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at CultureNotFoundExampleApp.App.Main()

Preview 5:


System.Globalization.CultureNotFoundException
  HResult=0x80070057
  Message=Culture is not supported.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride)
   at System.Globalization.CultureInfo..ctor(Int32 culture)
   at System.Windows.Input.InputLanguageSource.get_CurrentInputLanguage()
   at System.Windows.Input.InputLanguageManager.get_CurrentInputLanguage()
   at System.Windows.Documents.TextSelection.EnsureCaret(Boolean isBlinkEnabled, Boolean isSelectionActive, CaretScrollMethod scrollMethod)
   at System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight()
   at System.Windows.Documents.TextEditor.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
   at System.Windows.Controls.Primitives.TextBoxBase.OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
   at System.Windows.UIElement.OnGotKeyboardFocusThunk(Object sender, KeyboardFocusChangedEventArgs e)
   at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
   at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
   at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
   at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
   at System.Windows.UIElement.Focus()
   at System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(TextEditor This)
   at System.Windows.Documents.TextEditorMouse.OnMouseDown(Object sender, MouseButtonEventArgs e)
   at System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at CultureNotFoundExampleApp.App.Main()

该提问来源于开源项目:dotnet/runtime

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • weixin_39526564 weixin_39526564 4月前

    Wow, that's quite a fast turnaround. I can confirm that both workarounds solve the issue, too.

    Neat, thanks again!

    点赞 评论 复制链接分享
  • weixin_39526564 weixin_39526564 4月前

    Sorry, I posted too quickly to see your reply about the list of keyboards! :)

    Here you go.

    Keyboard quick-switch: keyboards

    Preferred languages: Preferred languages

    English language configuration/keyboard list: US

    Swedish language configuration/keyboard list: Sweden

    I really appreciate the help, thanks!

    点赞 评论 复制链接分享
  • weixin_39922929 weixin_39922929 4月前

    We'll work on fixing this issue. for now the workaround for that either you use a different keyboard (e.g en-US for instance) or you can add the following to your csproj file to enable the old behavior

    XML
    <itemgroup>
      <runtimehostconfigurationoption include="System.Globalization.UseNls" value="true"></runtimehostconfigurationoption>
    </itemgroup>
    
    点赞 评论 复制链接分享
  • weixin_39526564 weixin_39526564 4月前

    Thanks for the quick reply!

    I have to admit I don't have much experience debugging framework libraries. So far I've managed to retrieve a more complete exception message: Message "Culture is not supported. (Parameter 'culture')\r\n8192 (0x2000) is an invalid culture identifier."

    I can also see that _invalidCultureId is set to 8192. When it comes to the culture variable itself (that is passed to the method) I haven't been able to get further than Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away.

    See the attached screenshot, if it helps: culture

    I'll try to do some more digging, if I'm able to retrieve a value from the actual parameter passed to the method I'll post another comment, even though I assume it'll probably say 8192.

    点赞 评论 复制链接分享
  • weixin_39922929 weixin_39922929 4月前

    This is useful enough. thanks for the info.

    点赞 评论 复制链接分享
  • weixin_39611275 weixin_39611275 4月前

    Tagging subscribers to this area: , , Notify danmosemsft if you want to be subscribed.

    点赞 评论 复制链接分享
  • weixin_39922929 weixin_39922929 4月前

    thanks for reporting the issue, Do you know the culture number passed to the method?

    
    at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride)
    

    initially, this looks WPF issue but will investigate more first.

    点赞 评论 复制链接分享
  • weixin_39922929 weixin_39922929 4月前

    Also, could you please send me the list of keyboards installed in your system?

    点赞 评论 复制链接分享

相关推荐