title | description | ms.date |
---|---|---|
Breaking change: BinaryFormatter disabled across most project types |
Learn about the .NET 7 breaking change in serialization where serialize and deserialize methods on BinaryFormatter now through an exception at run time. |
05/01/2023 |
The xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize%2A?displayProperty=nameWithType and xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=nameWithType methods now through a xref:System.NotSupportedException at run time across nearly all project types, including console applications.
In .NET 7, the xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize%2A?displayProperty=nameWithType and xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize%2A?displayProperty=nameWithType methods were marked obsolete and raised an error at compile time. However, if your application suppressed the obsoletion, it could still call the methods and they functioned properly in most project types (excluding ASP.NET, WASM, and MAUI). For example, the APIs functioned correctly in a console app.
Starting in .NET 8, the affected methods throw a xref:System.NotSupportedException at run time across all project types except Windows Forms and WPF. The APIs continue to remain obsolete (as error) across all project types, including Windows Forms and WPF.
.NET 8 Preview 4
This change is a behavioral change.
This run-time change is the next stage of the BinaryFormatter obsoletion plan, in which xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter will eventually be removed from .NET.
The best course of action is to migrate away from BinaryFormatter
due to its security and reliability flaws.
However, should you need to continue using BinaryFormatter
, you can set a compatibility switch in your project file to re-enable BinaryFormatter
functionality. For more information, see the Recommended action section of the .NET 7 breaking change notification. That compatibility switch continues to be honored in .NET 8.
- xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(System.IO.Stream,System.Object)?displayProperty=fullName
- xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream)?displayProperty=fullName