I have an app, which uses a binary file for storage. It was working fine before i was forced to move it to a different PC.
Apparently, providing a SerializationBinder is supposed to be the solution for this exact problem...
All that has changed is the environment the app. is running in.
I found this...
The standard NET binary serializer is not well suited for data exchange between 2 different assemblies. When you go to deserialize, you'll get an an error similar to [Culture].[Assembly].[Version].SourceClass cannot be deserialized to [Culture].[Assembly].[Version].DestClass. This will happen even if the classes are identical.
There are several ways around this. A) Use the same service DLL on both sides to do the serializing B) trick it into deserializing by using an override to report a matching Culture-Assembly-Version-Class, but that seems dodgy or C) use XML serialization, but that makes for very wordy output, which is also readable.
How can I do that?
Apparently, providing a SerializationBinder is supposed to be the solution for this exact problem...
Code:
Imports System.Runtime.Serialization
'Imports System.Runtime.Serialization.Formatters.Binary
Public Class versionsSerializationBinder
Inherits SerializationBinder
Public Overrides Function BindToType(ByVal assemblyName As String, ByVal typeName As String) As System.Type
'abCore.Person, Address Book, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
'System.Collections.Generic.List`1[[abCore.Person, Address Book, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Return Type.GetType(String.Format("{0}, {1}", "System.Collections.Generic.List`1[[abCore.Person, Address Book, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]", _
"Address Book, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"))
End Function
End Class
Code:
If IO.File.Exists("addresses.bin") Then
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter.Binder = New versionsSerializationBinder
Dim fs As New IO.FileStream("addresses.bin", IO.FileMode.Open)
people = DirectCast(formatter.Deserialize(fs), List(Of Person))
fs.Close()
End If
I found this...
Quote:
The standard NET binary serializer is not well suited for data exchange between 2 different assemblies. When you go to deserialize, you'll get an an error similar to [Culture].[Assembly].[Version].SourceClass cannot be deserialized to [Culture].[Assembly].[Version].DestClass. This will happen even if the classes are identical.
There are several ways around this. A) Use the same service DLL on both sides to do the serializing B) trick it into deserializing by using an override to report a matching Culture-Assembly-Version-Class, but that seems dodgy or C) use XML serialization, but that makes for very wordy output, which is also readable.