old project is .net 2.0 now want to 4.6.1 but BinaryFormatter result is different. please help me fix it.
Asked
Active
Viewed 119 times
0
-
Don't use BinaryFormatter to begin with. Apart from breaking each time the runtime changes, it's insecure, considered obsolete for at least a decade and marked for removal – Panagiotis Kanavos Nov 24 '20 at 09:22
-
2As for what broke irretrievably in this case, the .NET 2.0 and .NET 4.0 runtimes are completely different. BinaryFormatter saves *everything* in a type, including type names and fields, so reloading a type that changed from one runtime version to the next is impossible. That's what makes it a security nightmare too - it can load arbitrary types from the binary file, allowing code injection – Panagiotis Kanavos Nov 24 '20 at 09:23
-
From the [BinaryFormatter security guide](https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide), the big yellow warning at the top of the document says `The BinaryFormatter type is dangerous and is not recommended for data processing. Applications should stop using BinaryFormatter as soon as possible, even if they believe the data they're processing to be trustworthy. BinaryFormatter is insecure and can't be made secure.` – Panagiotis Kanavos Nov 24 '20 at 09:25
-
Files serialized with BinaryFormatter (version 2.0) would probably be not readable by BinaryFormatter (version 4.6.1). Such files are very cumbersome and risky to deal with. Code, which uses them tends to break easily. Please read: https://learn.microsoft.com/en-GB/dotnet/standard/serialization/binaryformatter-security-guide - maybe you could migrate to a better alternative, for example System.Text.Json – greenmarker Nov 24 '20 at 09:26
-
This isn't a `maybe you should migrate`. It's a *you have to migrate*. It's been 10 years since .NET 4.0 came out. – Panagiotis Kanavos Nov 24 '20 at 09:29
-
A far better, far faster alternative nowadays would be to use protobuf through eg [protobut-net](https://github.com/protobuf-net/protobuf-net). protobuf is an efficient binary format, widely used in gRPC, allows you to specify the type format in an IDL file, which means it's cross-platform, doesn't break from application/runtime changes, has a ton of tools and libraries that work with it. – Panagiotis Kanavos Nov 24 '20 at 09:40
-
old project is for send tcp data ,it have been work for ten years ,ok,now i will change it to socket or others – lee Nov 25 '20 at 10:22
-
See also [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Nov 29 '20 at 14:38