Skip to content

Latest commit

 

History

History
45 lines (27 loc) · 3.31 KB

binaryformatter-disabled.md

File metadata and controls

45 lines (27 loc) · 3.31 KB
title description ms.date
Breaking change: BinaryFormatter disabled across most project types
Learn about the .NET 8 breaking change in serialization where serialize and deserialize methods on BinaryFormatter now throw an exception at run time.
05/01/2023

BinaryFormatter disabled across most project types

The xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(System.IO.Stream,System.Object)?displayProperty=nameWithType and xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream)?displayProperty=nameWithType methods now throw a xref:System.NotSupportedException at run time across nearly all project types, including console applications.

Previous behavior

In .NET 7, the xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(System.IO.Stream,System.Object)?displayProperty=nameWithType and xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(System.IO.Stream)?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.

New behavior

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.

Version introduced

.NET 8 Preview 4

Type of breaking change

This change is a behavioral change.

Reason for 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.

Recommended action

The best course of action is to migrate away from BinaryFormatter due to its security and reliability flaws. For more information, see Preferred alternatives.

If 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.

Affected APIs

  • 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

See also