DeSerialization seems to work in debug but not release?

R

Rob P

Hi All,

I'm currently writing an ancient wargame simulator in C# (see
http://ancientarmies.spaces.live.com/blog/) but seem to be having issues
with deserialization.

I have a bit of code that looks like:

public MapEditor.ObjectsToSerialize DeSerializeMap(string filename)
{
MapEditor.ObjectsToSerialize objectsToSerialize;
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
objectsToSerialize =
(MapEditor.ObjectsToSerialize)bFormatter.Deserialize(stream);
stream.Close();
return objectsToSerialize;
}

This code works fine in debug build, but if I compile to release build it
fails with
'An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.
'

What I'm finding confusing is why it should work in debug, but not release?
Has anyone else seen similar issues?

The project is coded in VS2005 using the 2.0 net framework.

Thanks
RobP
 
M

Morten Wennevik [C# MVP]

Rob P said:
Hi All,

I'm currently writing an ancient wargame simulator in C# (see
http://ancientarmies.spaces.live.com/blog/) but seem to be having issues
with deserialization.

I have a bit of code that looks like:

public MapEditor.ObjectsToSerialize DeSerializeMap(string filename)
{
MapEditor.ObjectsToSerialize objectsToSerialize;
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
objectsToSerialize =
(MapEditor.ObjectsToSerialize)bFormatter.Deserialize(stream);
stream.Close();
return objectsToSerialize;
}

This code works fine in debug build, but if I compile to release build it
fails with
'An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.
'

What I'm finding confusing is why it should work in debug, but not release?
Has anyone else seen similar issues?

The project is coded in VS2005 using the 2.0 net framework.

Thanks
RobP

Hi Rob,

Do you have an InnerException? TargetInvocationException is just a wrapper
exception and the TargetInvocationException.InnerException contains the real
exception.
 
R

Rob P

Morten Wennevik said:
Hi Rob,

Do you have an InnerException? TargetInvocationException is just a
wrapper
exception and the TargetInvocationException.InnerException contains the
real
exception.

Thanks Morten,

Will take a look at the inner exception. Tbh I've had quite a few issues
with serialization between different apps in .net and may well look at
saving the data manually using home grown code. This will up the code a
little but will significantly reduce the risk I'm exposed to by using
serialization.

Regards
RobP
 
M

Morten Wennevik [C# MVP]

Rob P said:
Thanks Morten,

Will take a look at the inner exception. Tbh I've had quite a few issues
with serialization between different apps in .net and may well look at
saving the data manually using home grown code. This will up the code a
little but will significantly reduce the risk I'm exposed to by using
serialization.

Regards
RobP

If you haven't already, take a look at XmlSerializer. As well as being
simpler to use (in best cases no additional code or attributes is needed to
the class to be serialized), it has the benefit of producing human readable
output so you can verify the serialized object is what you exected it to be.
It may not be able to serialize everything binary serialization can, but so
far I haven't found that to be a problem. It does result if much larger
files.
 

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