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.

NotSupportedException when deserializing DBNull from RESX

See original GitHub issue

Description

Hello,

My name is Norton and I’m writing on behalf of the WinForms team at DevExpress.

We came across the following issue: If DBNull.Value is assigned to a property of a component in the WinForms designer, it is stored in the RESX file associated with the form. If the same form is opened afterward, a SerializationException is thrown on an attempt to call the ResourceManager.GetObject method in the designer-generated code.

Reproduction Steps

  1. Install Visual Studio 2022 with the “.NET desktop development” workload.
  2. Open Visual Studio 2022 and create a new project using the “Windows Forms App [C#]” template (further template configuration doesn’t matter).
  3. Once the project is created, close the designer tab without waiting for it to load.
  4. Open Form1.Designer.cs and add this.Tag = DBNull.Value; at the end of the InitializeComponent method.
  5. Open the form designer by double-clicking Form1.cs in the Solution Explorer.
  6. Resize the form, save the file and close the tab. This forces the form designer to regenerate Form1.Designer.cs, store DBValue.Null in Form1.resx and generate a call to the ResourceManager.GetObject method for that value.
  7. Run the project.

I attached a project created using these steps - it is sufficient to simply run it. T1143850c.zip

These steps do not represent a realistic use case where this issue occurs, but the fastest way to reproduce it. A realistic use case would involve creating a custom control with a custom property initialized with DBNull.Value. If necessary, I can add steps and a project for this use case.

Expected behavior

The application launches without any exceptions and an empty form is displayed.

Actual behavior

An exception is thrown.

Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll
Only one DBNull instance may exist, and calls to DBNull deserialization methods are not allowed.

Regression?

This issue does not occur in .NET Framework.

Known Workarounds

We are not aware of workarounds that do not sacrifice usability (such as disabling serialization of the property completely with DesignerSerializationVisibilityAttribute).

Configuration

No response

Other information

We are not certain whether the issue lies in the way the WinForms designer or .NET SDK serializes the value, or the way the standard .NET serialization API serializes/deserializes it, but we consider the latter more likely, based on our findings. If this happens to be an issue with a different product, please let me know.

It’s also possible that this use case is not supported in general, although we have several reasons to believe why this is not the case:

  • The same use case works as expected in .NET Framework applications (i.e. this behavior is a regression).
  • With this limitation, it isn’t possible to define a serializable component property whose default value is DBNull.Value, which we consider a valid use case.
  • The DBNull type is still marked with SerializableAttribute, and it’s stated in https://github.com/dotnet/runtime/issues/23253 that support for its serialization was explicitly added.

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
TheLeftExitcommented, Feb 7, 2023

@ericstj I do, so that shouldn’t be a problem (aside from other considerations).

1reaction
TheLeftExitcommented, Feb 7, 2023

Thanks everyone. So, from what I understand, it is related to the WinForms designer after all.

Should I re-open this issue in the designer repo? Or, better yet, is it possible to move the issue there?

P.S. Case in point, the issue can also be reproduced with the .NET Framework out-of-process designer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NotSupportedException when deserializing DBNull from ...
We came across the following issue: If DBNull.Value is assigned to a property of a component in the WinForms designer, it is stored...
Read more >
Error: Only one DBNull instance may exist, and calls to ...
Line 1046, position 5. NotSupportedException: Only one DBNull instance may exist, and calls to DBNull deserialization methods are not allowed.
Read more >
C# JSON Deserialization System.NotSupportedException
Your exception says that you can't deserialize classes that don't have a default (parameterless) constructor.
Read more >
resourcemanager.cs - Reference Source - Microsoft
Users should ideally create a resource file for every culture, or // at least a ... no need to serialize this; just create...
Read more >
MySQL Connector/NET Release Notes
This fix omits the initial error message to streamline the connection behavior. (Bug ... Microseconds were deserialized incorrectly when MySqlCommand.
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