Serialization problem between 2 apps

M

Marc

Hi,
I am trying to serialize a data structure -- a list (of custom class)
-- in one application, then read it in with another application. My
serialize and deserialize subs are in a module that is shared between
the two applications, so they are using exactly the same code (this
module also contains the class, so I am certain the class code is the
same)

The code I'm using is quite simple, so I'm not sure where I'm going
wrong:

Private AllOrders as List (Of CustomOrder)

Function SerializeOrders(ByVal Path As String) As Integer
'Writes (serializes) All Orders to disk at given path
Dim myFileStream As New System.IO.FileStream(Path,
System.IO.FileMode.Create)
Dim MyFormatter As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

If (AllOrders IsNot Nothing) Then
MyFormatter.Serialize(myFileStream, AllOrders)
End If
myFileStream.Close()
End Function

Function DeserializeOrders(ByVal path As String) As Integer
' Reads list of orders from given path and OVERWRITES AllOrders
Dim myFileStream As New System.IO.FileStream(path,
System.IO.FileMode.OpenOrCreate)
Dim MyFormatter As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

Dim GenericObject As Object
Try
GenericObject = MyFormatter.Deserialize(myFileStream)
AllOrders = CType(GenericObject, List(Of TEOrder))
Catch ex As Exception
Finally
myFileStream.Close()
End Try
End Function

No exception is ever raised. When I serialize, I can check the file
and it contains data (which means that serialization is working).
However, when I deserialize in the other application, I get 0 objects.

A final observation: serialization & deserialization work fine when
done within the same application. It is only between applications
that I have the problem.

Help?

BTW I would be interested in using the SOAPFormatter but I can't find
it in VB.NET. I read about it online but don't see it available.
What do I need to do to make that available to me? (I would much
prefer human-readable output when I serialize)

Thanks,
Marc
 
C

Chris Dunaway

Hi,
I am trying to serialize a data structure -- a list (of custom class)
-- in one application, then read it in with another application. My
serialize and deserialize subs are in a module that is shared between
the two applications, so they are using exactly the same code (this
module also contains the class, so I am certain the class code is the
same)

What does this mean, exactly? Have you placed your serialization code
in a class library and both applications reference that library? Or do
both applications simply share the same code file? If they share the
same code file, then that is likely your problem. Even if they have
identical code, the classes are still considered different types and
the second application will not be able to deserialize the file that
is serialized by the first.

Try
GenericObject = MyFormatter.Deserialize(myFileStream)
AllOrders = CType(GenericObject, List(Of TEOrder))
Catch ex As Exception
Finally
myFileStream.Close()
End Try
End Function

No exception is ever raised. When I serialize, I can check the file

As the other poster pointed out, you are catching and swallowing the
exception.
BTW I would be interested in using the SOAPFormatter but I can't find
it in VB.NET. I read about it online but don't see it available.
What do I need to do to make that available to me? (I would much
prefer human-readable output when I serialize)

The SoapFormatter is defined in the
System.Runtime.Serialization.Formatters.Soap namespace which is
contained, coincidentally, in
System.Runtime.Serialization.Formatters.Soap.dll. Add a reference to
that assembly and you can get at the SoapFormatter.

Chris
 
M

Marc

What does this mean, exactly? Have you placed your serialization code
in a class library and both applications reference that library? Or do
both applications simply share the same code file? If they share the
same code file, then that is likely your problem. Even if they have
identical code, the classes are still considered different types and
the second application will not be able to deserialize the file that
is serialized by the first.




As the other poster pointed out, you are catching and swallowing the
exception.




The SoapFormatter is defined in the
System.Runtime.Serialization.Formatters.Soap namespace which is
contained, coincidentally, in
System.Runtime.Serialization.Formatters.Soap.dll. Add a reference to
that assembly and you can get at the SoapFormatter.

Chris

Thanks Chris (and Schneider)

I decided to investigate the exceptions a little more and realized
that because I was simply using two copies of the same class files,
the namespaces were not shared and I could not share objects. Thus I
have created a class library which is used by both applications now,
and I am gleefully sharing objects.

(Then I checked the newgroup and found Chris's reply, which would have
saved me a lot of time if it had come a few hours earlier!)

Also thanks for the SoapFormatter information.

Regards,
Marc
 

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

Similar Threads


Top