Deserialization issue

J

Joe

I'm getting an error when deserializing my objects:
"The ObjectManager found an invalid number of fixups. This usually indicates
a problem in the Formatter."

I added a new object to a class that gets serialized and put the
OptionalField attribute on that object. When trying to deserialize my object
in an older version of our application that doesn't have this new class I
get the above error but that's only when items have been added to the class.

The class looks like this:
public class MyCollection : BindingList<MyClass>
{

}

public class MyClass
{
private string mystring;
private CustomEnum myenum1;
private CustomEnum2 myenum2;
}

public class ClassThatGetsSerialized
{
string member1;
int member2;
...
...
[OptionalField]
MyCollection mycollection;

....
}

The error only occurs if object ClassThatGetsSerialized is deserialized by
an older version that doesn't have the definition for MyCollection and if
the member of ClassThatGetsSerialized.mycollection has items in it. If there
are no items then the error does not happen.

We use the OptionalField attribute in many places and it always works fine.

Any ideas?

Thanks,
Joe
 
J

Jialiang Ge [MSFT]

Hello Joe,

Thank you for reporting this issue to us. I've reproduced the symptom on my
side with your instructions:

**** App that serialize the object ****

[Serializable]
public class MyCollection : BindingList<MyClass>
{
}
[Serializable]
public class MyClass
{
private string mystring = "test";
}
[Serializable()]
class ClassThatGetsSerialized
{
public ClassThatGetsSerialized()
{
mycollection.Add(new MyClass());
}

[OptionalField]
MyCollection mycollection = new MyCollection();

public void Save()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\test.txt",
System.IO.FileMode.Create);

IFormatter oFormatter = (IFormatter)new BinaryFormatter();
oFormatter.Serialize(oStream, this);
oStream.Close();
}
}

ClassThatGetsSerialized obj = new ClassThatGetsSerialized();
obj.Save();

**** App that deserialize the object ****

[Serializable()]
class ClassThatGetsSerialized
{
public static ClassThatGetsSerialized LoadObject()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\test.txt",
System.IO.FileMode.Open);
IFormatter oFormatter = (IFormatter)new BinaryFormatter();
ClassThatGetsSerialized obj =
(ClassThatGetsSerialized)oFormatter.Deserialize(oStream);
oStream.Close();
return obj;
}
}

ClassThatGetsSerialized obj = ClassThatGetsSerialized.LoadObject();

When I run the Deserialization app after running the Serialization one, I
get the exception "The ObjectManager found an invalid number of fixups.
This usually indicates a problem in the Formatter". The callstack is:

at System.Runtime.Serialization.ObjectManager.DoFixups()
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(Head
erHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(S
tream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(S
tream serializationStream)
at ConsoleApplication1.ClassThatGetsSerialized.LoadObject()
at ConsoleApplication1.Program.Main(String[] args)

Joe, I suggest your submitting this issue to our feedback site, where the
product group will further look into it:
http://connect.microsoft.com/VisualStudio
It's appreciated if you paste the feedback link here to benefit the
community.

Thanks
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

Joe

Are there any workarounds for this?

"Jialiang Ge [MSFT]" said:
Hello Joe,

Thank you for reporting this issue to us. I've reproduced the symptom on
my
side with your instructions:

**** App that serialize the object ****

[Serializable]
public class MyCollection : BindingList<MyClass>
{
}
[Serializable]
public class MyClass
{
private string mystring = "test";
}
[Serializable()]
class ClassThatGetsSerialized
{
public ClassThatGetsSerialized()
{
mycollection.Add(new MyClass());
}

[OptionalField]
MyCollection mycollection = new MyCollection();

public void Save()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\test.txt",
System.IO.FileMode.Create);

IFormatter oFormatter = (IFormatter)new BinaryFormatter();
oFormatter.Serialize(oStream, this);
oStream.Close();
}
}

ClassThatGetsSerialized obj = new ClassThatGetsSerialized();
obj.Save();

**** App that deserialize the object ****

[Serializable()]
class ClassThatGetsSerialized
{
public static ClassThatGetsSerialized LoadObject()
{
System.IO.FileStream oStream = new
System.IO.FileStream("c:\\test.txt",
System.IO.FileMode.Open);
IFormatter oFormatter = (IFormatter)new BinaryFormatter();
ClassThatGetsSerialized obj =
(ClassThatGetsSerialized)oFormatter.Deserialize(oStream);
oStream.Close();
return obj;
}
}

ClassThatGetsSerialized obj = ClassThatGetsSerialized.LoadObject();

When I run the Deserialization app after running the Serialization one, I
get the exception "The ObjectManager found an invalid number of fixups.
This usually indicates a problem in the Formatter". The callstack is:

at System.Runtime.Serialization.ObjectManager.DoFixups()
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(Head
erHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(S
tream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(S
tream serializationStream)
at ConsoleApplication1.ClassThatGetsSerialized.LoadObject()
at ConsoleApplication1.Program.Main(String[] args)

Joe, I suggest your submitting this issue to our feedback site, where the
product group will further look into it:
http://connect.microsoft.com/VisualStudio
It's appreciated if you paste the feedback link here to benefit the
community.

Thanks
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.
=================================================
 
J

Jialiang Ge [MSFT]

Hello Joe,

The old client app expects to know the type of MyClass.
I find that a possible workaround in this issue is to declare MyClass as a
struct (value type), instead of a class:

public struct MyClass
{
private string mystring;
private CustomEnum myenum1;
private CustomEnum2 myenum2;
}

In this way, the old apps continue working fine. Please have a try and let
me know whether the workaround is helpful to you.

Have a nice weekend!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

Joe

Sorry about taking so long to get back.

Using a struct only sort of works. It does not make it easy for data binding
to a data grid.
 
J

Joe

My solution was to re-write MyCollection class as MyCollection :
CollectionBase, IBindingList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top