SOAP serialization exception

T

Tim Anderson

I have an app that uses the SOAP serializer to serialize and deserialize a
object of a certain class to a file. The class implements ISerializable.
From time to time it is necessary to add or remove fields from the class.
I've been able to deserialize out-of-date versions by handling serialization
exceptions in Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext).

I've now hit a problem. With the latest version, if I try to deserialize an
out-of-date XML file I get an exception *before* my constructor starts to
run. Visual Studio reports:

A first chance exception of type
'System.Runtime.Serialization.SerializationException' occurred in
system.runtime.serialization.formatters.soap.dll
The message is: Top Object cannot be instantiated for element 'mListColor'.

mListColor is a field that no longer exists; however even if I do the
obvious thing and add it back, I still get the same error.

The serialization and deserialization works fine with newly serialized
files; it is just deserializing the old version that is not working.

How can I troubleshoot this?

Thanks for any advice,

Tim
 
K

Kevin Yu [MSFT]

Hi Tim,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when your class structure changes, your
app cannot deserialize an old version of Xml file that was serialized. If
there is any misunderstanding, please feel free to let me know.

Generally, I think this exception was thrown because you have changed the
members for your class. However, I think changing the structure back to
when the XML was created should make deserialization work. Could you please
try to check your code to see if you have forgotten any other things in the
class?

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

Tim Anderson

Kevin Yu said:
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when your class structure changes,
your
app cannot deserialize an old version of Xml file that was serialized. If
there is any misunderstanding, please feel free to let me know.

That's not quite right. The scenario is:

An object of classv1 is serialized to an xml file using the SOAP serializer
A new version of the app has a modified version of classv1, lets call it
classv2 (the actually class name is the same)
I now want to deserialize the xml file to an object of classv2.

Obviously there are different members in classv2. However, by implementing
ISerializable you can handle this situation by deserializing as far as you
can (in the overridden constructor) and catching the SerializationException.
You can then assign default values to the unserialized members.

This technique has worked well for several versions of the class. However,
it is now failing with the following exception *before* my constructor
starts to
run. Visual Studio reports:

A first chance exception of type
'System.Runtime.Serialization.SerializationException' occurred in
system.runtime.serialization.formatters.soap.dll
The message is: Top Object cannot be instantiated for element 'mListColor'.

mListColor is a field that no longer exists; however even if I do the
obvious thing and add it back, I still get the same error.

The problem here is that I can't deserialize *any* of the pre-existing XML
file.

Since we don't have the source for the SOAP serializer, a good starting
point would be what this "Top Object" exception actually means?

Thanks,

Tim
 
K

Kevin Yu [MSFT]

Hi Tim,

Thanks for clarify the issue. To troubleshoot this issue, we have to step
into the ISerializabe.GetObjectData method to see which line throws the
SerializationException. When you find the line of code, please check if it
is in the Try block. If the code is in Try block, it has to be catched and
continue deserializing.

If that still doesn't help, could you please send me a repro by email? It
is better if you also attach a former version that works fine. Remove
'online' from the no spam alias is my real email.

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

Tim Anderson

Kevin Yu said:
Hi Tim,

Thanks for clarify the issue. To troubleshoot this issue, we have to step
into the ISerializabe.GetObjectData method to see which line throws the
SerializationException. When you find the line of code, please check if it
is in the Try block. If the code is in Try block, it has to be catched and
continue deserializing.

I'm a bit confused by this. GetObjectData is called for serialization, not
deserialization. I'm getting the exception when deserializing.

The equivalent code would be the constructor. However, as I mentioned, the
exception is thrown before the code in the constructor runs, hence my
problem.

Tim
 
K

Kevin Yu [MSFT]

Hi Tim,

You're right. GetObjectData is used for serialization.

It's hard to troubleshoot this issue without a repro. Could you please
provide me with a repro package with the mListColor member and still throws
an exception? Remove 'online' from the no spam alias is my real email
address.

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

Tim Anderson

Kevin Yu said:
Hi Tim,

You're right. GetObjectData is used for serialization.

It's hard to troubleshoot this issue without a repro. Could you please
provide me with a repro package with the mListColor member and still
throws
an exception? Remove 'online' from the no spam alias is my real email
address.

I've now found a workaround. The problem occurred because of changes I made
to the type of one of the fields being serialized. I amended this type to
implement ISerializable. I then got the "top object" exception. I've
reverted this type not to implement ISerializable and the deserialization of
an old version now works as expected.

Thanks,

Tim
 

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