How can I use BinaryFormatter in .net 8 application

Aman Agrahari 20 Reputation points
2025-04-21T10:47:18.7566667+00:00

I'm migrating .net application from .net 4.7.2 to .net 8. I had used BinaryFormatter for serialization and deserialization in my application but it is obsolete in .net 8.

Still I want to use binaryFormatter in upgraded .net 8 application temporarily untill I test my upgraded application is working fine.

serialization and deserialization functions are written in vb.net

Kindly suggest me detailed step by step solution.

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,230 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shivanshu Sharma 0 Reputation points
    2025-05-03T11:16:10.2733333+00:00

    You can use the below approach for this as i found earlier online-

    1. Update Your Project File

    .NET 8 disables BinaryFormatter by default. To re-enable it, add the following property in your project file (your .vbproj file)

    This setting instructs the runtime to allow BinaryFormatter even though it’s obsolete.
    2. Suppress Compile-Time Warnings in Code

    Even after enabling the compatibility switch, you may still see compile-time warnings (SYSLIB0011). You can suppress these warnings locally by surrounding your serialization/deserialization calls with pragma directives.
    3. Test Your Application Thoroughly

    After making these changes, rebuild your project and run your tests. Verify that your serialization and deserialization functions behave as in your original .NET Framework 4.7.2 application.

    1. Plan for a Migration

    Even though you can re-enable BinaryFormatter temporarily:

    Security Risks: BinaryFormatter is inherently insecure, which is why Microsoft deprecated it. Any exposed deserialization functionality could be abused by attackers.

    Future Compatibility: .NET 9 will remove BinaryFormatter entirely. You should plan to switch to a modern serialization method.

    Recommended Alternatives Include:

    • System.Text.Json: Offers good performance and is supported by Microsoft.

    Newtonsoft.Json: A very popular alternative that provides richer features.

    MessagePack: If you require binary serialization with better performance and a compact binary representation.

    1. Test Your Application Thoroughly

    After making these changes, rebuild your project and run your tests. Verify that your serialization and deserialization functions behave as in your original .NET Framework 4.7.2 application.

    1. Plan for a Migration

    Even though you can re-enable BinaryFormatter temporarily:

    Security Risks: BinaryFormatter is inherently insecure, which is why Microsoft deprecated it. Any exposed deserialization functionality could be abused by attackers.

    Future Compatibility: .NET 9 will remove BinaryFormatter entirely. You should plan to switch to a modern serialization method.

    Recommended Alternatives Include:

    • System.Text.Json: Offers good performance and is supported by Microsoft.
    • Newtonsoft.Json: A very popular alternative that provides richer features.
    • MessagePack: If you require binary serialization with better performance and a compact binary representation.
    • For each alternative, the migration involves: Changing your serialization code to use the new library.
      Adjusting your data contracts (if required).
      
         Testing to ensure data integrity and performance are acceptable.
      
    0 comments No comments

Your answer