dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.36k stars 967 forks source link

Confusing Error Message Surfaces During Clipboard Operation #11682

Closed lonitra closed 2 months ago

lonitra commented 2 months ago

Background: When BinaryFormatter is disabled (EnableUnsafeBinaryFormatterSerialization = false) and users use the clipboard to set/get an object that would historically use BinaryFormatter, we would serialize the exception message onto the clipboard instead of their desired object in an effort to give feedback to users of failure.

Issue: In the latest build with a default WinForms app, when users set/get an object onto the clipboard that would historically use BinaryFormatter to serialize, users will get error message string "Using the BinaryFormatter is not supported in trimmed applications" serialized to the clipboard. This is confusing as trimming is not on by default and they should really be getting error message "BinaryFormatter serialization and deserialization have been removed. See https://aka.ms/binaryformatter for more information." if BinaryFormatter nuget package has not been added.

How it works today: When placing an item onto the clipboard that historically uses BinaryFormatter to serialize, the trimming exception is thrown at https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/src/System/Windows/Forms/OLE/DataObject.ComposedDataObject.NativeDataObjectToWinFormsAdapter.cs#L212. That exception will get caught at https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/src/System/Windows/Forms/OLE/DataObject.ComposedDataObject.WinFormsDataObjectToNativeAdapter.cs#L129 and the exception will get serialized onto the clipboard instead.

Switch currently checks if EnableUnsafeBinaryFormatterSerialization has been enabled (https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/src/System/Windows/Forms/OLE/DataObject.ComposedDataObject.NativeDataObjectToWinFormsAdapter.cs#L25), but it seems we additionally need a way to determine if user has turned on trimming before deciding to surface this error message

Simple repro app: WinFormsApp73.zip

  1. Place breakpoint on line 13 in Form1.cs and run/debug app.
  2. Step once to allow line 13 to execute and observe error message about trimming in test variable

Note: When setting EnableUnsafeBinaryFormatterSerialization = true in the .csproj, the correct error message is displayed

lonitra commented 2 months ago

cc: @LakshanF

John-Qiao commented 1 month ago

Verified this issue on 9.0.100-preview.7.24371.4 with dlls built from winforms repo of main branch, it was fixed: the correct error message is displayed When setting EnableUnsafeBinaryFormatterSerialization = true.

11682-testresult

LakshanF commented 1 month ago

@John-Qiao, thanks for taking a look at this! The error message should be on the test variable, and the message should not mention trimming.

John-Qiao commented 1 month ago

@LakshanF I have updated above test result, please check it.

Nora-Zhou01 commented 1 month ago

Verified the issue with .NET 9.0.100-preview7.24402.8 test pass build that the issue has been fixed, which have the same results as above.

IrinaPykhova commented 3 weeks ago

This week I'm looking at drag&drop behavior in control which historically used binaryformatter. Current version doesn't use binaryformatter any more, but it still doesn't work in the same way with .Net 8 version. I tried to debug it or catch handled exceptions. In some situations in .Net 9 Preview 7 I still can catch handled exception with mesage "Using the BinaryFormatter is not supported in trimmed applications" coming from some MS code. And as soon as VS catches it in the middle of drag&drop, it hangs forever and I can't go any further with my research. Is that expected? Why at all this happens if custom code doesn't use binaryformatter? Is there any chance you forgot it in some place?

LakshanF commented 3 weeks ago

@IrinaPykhova, can you check if you are using the latest .NET 9 Preview builds please? The PR 11689, removed the reference to trimming when using the BinaryFormatterand the exception messages no longer should have any references to BinaryFormatter and Trimming. i.e. newer .NET 9 builds should not have this issue.

IrinaPykhova commented 3 weeks ago

@IrinaPykhova, can you check if you are using the latest .NET 9 Preview builds please? The PR 11689, removed the reference to trimming when using the BinaryFormatterand the exception messages no longer should have any references to BinaryFormatter and Trimming. i.e. newer .NET 9 builds should not have this issue.

I use dotnet-sdk-9.0.100-preview.7.24407.12, suppose it's the last one which is publicly available