Skip to content

[release/10.0] AppContext switch for restoring exception handling behavior in WPF Clipboard APIs - #15037

Merged
Shyam-Gupta merged 1 commit into
dotnet:release/10.0from
Shyam-Gupta:cbSwitch10.0
Sep 4, 2026
Merged

[release/10.0] AppContext switch for restoring exception handling behavior in WPF Clipboard APIs#15037
Shyam-Gupta merged 1 commit into
dotnet:release/10.0from
Shyam-Gupta:cbSwitch10.0

Conversation

@Shyam-Gupta

@Shyam-Gupta Shyam-Gupta commented Sep 3, 2026

Copy link
Copy Markdown
Member

Fixes empty string scenario in bug #14322

Description

Until .NET 9.0, the WPF Clipboard Get* APIs threw exceptions when an operation failed. This differed from the WinForms Clipboard APIs, which swallow exceptions and return an empty string.

In .NET 10, we unified the clipboard implementation in System.Private.Windows.Core. The unified implementation follows the existing WinForms behavior, but this introduces a behavior change for WPF: its Clipboard APIs now also swallow exceptions and return an empty string instead of propagating the exception.

Customer Impact

This behavior change is causing issues for some existing WPF applications that rely on exceptions being thrown to detect clipboard operation failures. More details are available in the bug.

Regression

Yes - In WPF from .NET 9.0

Fix

Added a new AppContext switch: "Windows.Clipboard.ThrowExceptionsForGetAPIs" When set, the Clipboard Get* APIs will throw exception during a Clipboard failure. This switch is off by default. In off state the APIs will continue to swallow exceptions and return an empty string.

Added unit tests.

Testing

Created a WinForms app to write unicode data on the clipboard and another app to read it. Before the changes, the reader app returned empty string very frequently. After fix and with the new AppContext switch turned on, the app did not return empty string for Get* APIs at all. Note that it still returns empty string if QueryGet* API fails which have same behavior as in .NET 9.

Risk

This should be a very low risk change as this behavior was working fine in 9.0 and previous versions.

Microsoft Reviewers: Open in CodeFlow

…ipboard APIs (dotnet#15022)

* Fixes empty string scenario in bug dotnet#14322

Fix: Added a new AppContext switch: "Windows.Clipboard.ThrowExceptionsForGetAPIs"
When set, the Clipboard Get* APIs will throw exception during a Clipboard failure. this switch is off by default.
In off state the APIs will continue to swallow exceptions and return an empty string.

Added unit tests.

* PR comments.

* Throwing exception for QueryGetData also.
@Shyam-Gupta Shyam-Gupta self-assigned this Sep 3, 2026
@Shyam-Gupta
Shyam-Gupta requested a review from a team as a code owner September 3, 2026 19:41
@Shyam-Gupta Shyam-Gupta added the area-Clipboard Issues related to Clipboard label Sep 3, 2026
@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.76471% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.10708%. Comparing base (9b469cd) to head (ae5d910).
⚠️ Report is 3 commits behind head on release/10.0.

Additional details and impacted files
@@                  Coverage Diff                   @@
##           release/10.0      #15037         +/-   ##
======================================================
- Coverage      77.11050%   77.10708%   -0.00343%     
======================================================
  Files              3274        3274                 
  Lines            645095      645169         +74     
  Branches          47709       47715          +6     
======================================================
+ Hits             497436      497471         +35     
- Misses           143976      144013         +37     
- Partials           3683        3685          +2     
Flag Coverage Δ
Debug 77.10708% <91.76471%> (-0.00343%) ⬇️
integration 18.98073% <8.10811%> (+0.00734%) ⬆️
production 51.92600% <91.89189%> (-0.00781%) ⬇️
test 97.40921% <91.66667%> (-0.00078%) ⬇️
unit 49.34527% <91.89189%> (-0.00794%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@KlausLoeffelmann KlausLoeffelmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say, OK.

2 Question though, which we can iterate too, if we do not have good answers for it:

  • When we tweak the behavior now for WPF - does that have any side effects on how the WinForms clipboard used to handle corner cases? (Or usual cases?)

  • If we change clipboard behavior with a context switch - what effects does that context switch have for a scenario, where a WPF App hosts a WinForms UserControl with a WinForms host control, or a WinForms Apps hosts a WPF UserControl with the ElemementHost.

(We do not need to address this now - and do this iteratively, to unblock customers as quick as possible. But let's also make sure, we're not introducing issues "for the other side" at the same time.)

@Shyam-Gupta

Copy link
Copy Markdown
Member Author
  • When we tweak the behavior now for WPF - does that have any side effects on how the WinForms clipboard used to handle corner cases? (Or usual cases?)

The default behavior of the WinForms Clipboard APIs will remain the same. The Get* APIs don't throw exceptions for .NET 9 and previous versions, and they will continue in the same manner going forward. Only when the new AppContext switch is turned on, the APIs will throw exception.

  • If we change clipboard behavior with a context switch - what effects does that context switch have for a scenario, where a WPF App hosts a WinForms UserControl with a WinForms host control, or a WinForms Apps hosts a WPF UserControl with the ElemementHost.

The AppContext switch is at app level. If it is turned on for a WPF or WinForms app, the Get* APIs will start throwing exceptions.

@Shyam-Gupta Shyam-Gupta added servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria servicing-approved .NET Shiproom approved the PR for merge and removed servicing-consider .NET Shiproom label indicating a PR seeks to enter into a branch under Tell-Mode criteria labels Sep 4, 2026
@Shyam-Gupta

Copy link
Copy Markdown
Member Author

Approved over email.

@Shyam-Gupta
Shyam-Gupta merged commit 39f118d into dotnet:release/10.0 Sep 4, 2026
10 checks passed
@Shyam-Gupta
Shyam-Gupta deleted the cbSwitch10.0 branch September 4, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Clipboard Issues related to Clipboard servicing-approved .NET Shiproom approved the PR for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants