question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Runtime crash due to localization and the way designer serializes keyboard shortcuts

See original GitHub issue
  • .NET Core Version: 3.1.2

  • Have you experienced this same bug with .NET Framework?: No

Problem description:

Steps to reproduce:

  • Create a blank .NET Framework WinForms application with a Form.
  • Set the Form to Localizable = true in the designer.
  • Add context menu with single item.
  • Set the ShortcutKeys on the item to Control+C.
  • Observe that the application works just fine on .NET Framework 4.8.
  • Switch locale at startup to German by System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
  • Observe that the application still starts just fine on .NET Framework 4.8.
  • Change the application to target netcoreapp31 and it immediately crashes on startup.

The reason for the crash is that the .resx file contains the shortcut key serialized as

  <data name="booToolStripMenuItem.ShortcutKeys" type="System.Windows.Forms.Keys, System.Windows.Forms">
    <value>Ctrl+C</value>
  </data>

Notice that the serialized value starts with Ctrl, not Control. It is produced and consumed by KeysConverter class that is locale dependent. While the value Control would work in any locale due to the fallback to names in the Keys enum the value Ctrl is taken from a dictionary that contains localized names. On .NET Core 3.1 in the German localization the name for Control key is Strg. Thus the value Ctrl+C in the resource file is invalid and causes conversion error at runtime.

Notably we migrated a large project from .NET Framework to .NET Core where we hit this issue. The .NET Framework also has similar issue in code but the localizations may differ which would explain why the problem is not hit.

Manually fixing the value in the .resx file to Control+C makes the application work in any locale but any further editing in the designer breaks it again.

Expected behavior:

No crash. The resources generated by the designer should be locale independent.

Minimal repro:

Here’s the sample app created using the steps above. It is multi-targeting both .NET Framework and .NET Core so it can run in both variation from inside Visual Studio.

WindowsFormsApp2.zip

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:33 (27 by maintainers)

github_iconTop GitHub Comments

4reactions
RalphLAtGitHubcommented, Jan 8, 2022

To whom it may concern at Microsoft: It is extremely annoying that Microsoft is unable to fix this bug after almost two years, let alone provide a timeline for solving the problem. if I did my job like that, I would be bankrupt!

3reactions
weltkantecommented, Feb 24, 2020

Desktop Framework:

  • VS designer interacts with the Property Grid in the Current Culture (OS language) but CurrentUICulture is the VS installation language - in other words the developer usually sees and edits the localized version in the designer (but for KeysConverter he will see keys localized according to his VS language because SR string resources in its implementation use CurrentUICulture not CurrentCulture)
  • VS designer stores in resx with invariant culture. This breaks for KeysConverter because it keeps operating on CurrentUICulture which still is the VS installation language.
  • msbuild deserializes resx entries with invariant culture. KeysConverter still uses CurrentUICulture which cancels out the bug when building from within VS, but it will fail the build when run outside VS and the OS and VS language does not match (one of my machines has english VS on german OS and could not build the test project outside VS on this machine)
  • runtime deserializes with BinaryFormatter (culture is irrelevant) so the bugs above never mattered for end users as long as your build environment had a consistent language (and if it didn’t you’d get build errors instead of runtime errors)

.NET Core

  • VS designer doesn’t support custom controls yet so I couldn’t check how CurrentCulture and CurrentUICulture are used
  • msbuild does not use TypeConverter, it just takes the string from resx
  • runtime deserializes with invariant culture

Asumming VS designer for .NET Core will be implemented analogous to Desktop Framework it will interact with the developer in the Current Culture and serialize to resx with Invariant Culture - then everything will play out fine for a correctly implemented 3rd party TypeConverter.

That leaves the broken WinForms KeysConverter to fix. The new resx writer implementation moves a build-time localization issue (which either canceled out itself or resulted in build failures) into runtime errors, you’ll have to fix KeysConverter and support serialization/deserialization with the requested culture, otherwise the new resx format will never work correctly with KeysConverter.

You also might have to consider providing a migration path, because after fixing KeysConverter the VS designer will no longer be able to read resx forms made on non-english VS installations (both made in .NET Core before the fix and those migrated from Desktop Framework will fail to open in the designer). Considering manual fixup of the resx files is possible you may get away without providing an automated resolution in the .NET Core VS designer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Runtime crash due to localization and the way designer ...
Windows Forms is a .NET UI framework for building Windows desktop applications. - Runtime crash due to localization and the way designer serializes...
Read more >
RuntimeError: Attempting to deserialize object on a CUDA ...
As you state the problem hints you are trying to use a cuda-model on non-cuda machine. Pay attention to the details of the...
Read more >
Re: Adobe Media Encoder CS4 runtime error
I ran Flash, checked and installed updates. Now, when I launch the Encoder -- either through the shortcut or through the Flash import...
Read more >
Debugging techniques and tools - Visual Studio (Windows)
Prepare your code for debugging by leveraging the IDE's code analyzer. How to fix exceptions (run-time errors). How to minimize bugs by coding ......
Read more >
Performance Improvements in .NET 7
NET 7 is fast. Really fast. This post deep-dives into hundreds of performance improvements that contributed to that reality.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found