dotnet / dotnet/wpf

Infinite Recursion Crash in WPF Window Containing WindowsFormsHost After Upgrading From .NET 6 to .NET 8

Open
#10,171 0 comments 0 reactions 1 assignee Claimed by @dipeshmsft View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

After upgrading a WPF application from .NET 6 to .NET 8, windows that contain a WindowsFormsHost element now cause a hard crash when the window is closed. Prior to the upgrade, these windows closed without error on .NET 6.

This issue only arises on windows that include a WindowsFormsHost. If the WindowsFormsHost is removed from the XAML or the entire window, the exception does not occur. No code changes other than upgrading the target framework from .NET 6 to .NET 8 were made.

Explicitly disposing of the WindowsFormsHost on closing resolves the issue.

Exception message: System.InvalidOperationException: Encountered infinite recursion while looking up resource '{key}' in {"System.Private.CoreLib"}. Verify the installation of .NET is complete and does not need repairing, and that the state of the process has not become corrupted.

### Reproduction Steps

- Create a new WPF application targeting .NET 8.
- Add a WindowsFormsHost control to a WPF Window.
- Add Forms control to WindowsFormsHost
- Run the application.
- Open the window that contains the WindowsFormsHost.
- Close the window.

### Expected behavior

The window should close cleanly without any exceptions, as it did on .NET 6.

### Actual behavior

The application throws an unhandled exception and crashes when closing the window containing a WindowsFormsHost on .NET 8.

### Regression?

This behaviour was not present in .NET 6.

### Known Workarounds

Removing the WindowsFormsHost from the window avoids the crash, but this is not feasible for the full application since we are in the process of migrating Windows Forms code to WPF incrementally.

Targeting .NET 6 instead of .NET 8 also avoids the issue, but we need .NET 8 for new features and performance improvements.

### Impact

This issue severely impacts application stability and user experience. Because the crash occurs when closing windows that contain critical embedded Windows Forms components, end-users experience sudden and unexplained application terminations. In a production environment, the crash undermines confidence in the software’s reliability and can negatively affect the application's reputation.

### Configuration

_No response_

### Other information

The issue seems to be machine dependant as some users are able to reproduce the exception while others are not. Those that can reproduce can reliably reproduce.

The issue does not appear to be related to any third-party libraries.

We have tried with clean installs of .NET 8 and verified no installation corruption.

The infinite recursion message suggests a resource lookup issue within System.Private.CoreLib, triggered specifically by shutting down a WPF window hosting WinForms controls.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.