Binary deserialize vb.net class in another .net project

I am sending some data from a 32 bit .net program over a socket connection (remote desktop if you're interested). I need to send some binary data (actually some basic emails + file attachments for Outlook). The email + subject is text data, but the file can / will be binary of course.

So I have a structure (or class):

<Serializable>
Public Class oEmail
   ' we serialize this into one Byte() object - send to client and then deserlize
 Public Email As String         ' outlook email
 Public Subject As String       ' outlook subject
 Public CC As String            ' outlook CC
 Public BC As String            ' outlook bc (blind copy)
 Public MessageText As String   ' message text
 Public AttachPath As String    ' path name to attachment (with \)
 Public AttachFileName As String
 Public FileDat As New Byte()    ' any kind of windows file - to be attached to outlook
End Class

      

Now binary serialization will "be" meow cats. I can take the above structure - serialize it to a byte array and just send it over the network channel to the client and then to the traffic jam.

For example:

Dim MyOutlook As New oEmail
MyOutlook = Deserialize(Of oEmail)(MyData)

      

And the deserialized code is REALLY simple:

Public Shared Function Deserialize(Of T)(ByVal data As Byte()) As T
        Dim binFormat = New BinaryFormatter()

        Using M As New IO.MemoryStream(data, False)
            Return CType((New BinaryFormatter).Deserialize(M), T)
        End Using

End Function

      

However, on the client side, this is a 64-bit application. It is also .net, but called by the RDP client as an external un-managed.dll, which in turn manages the managed code. To make it a bit REALLY long story, I cannot add or use SAME.dll for the above class structure on the client software side.

So when I try to deserialize on the client side I of course get this LAST nasty message:

Cannot find assembly "RDPSERVER"; Version 1.0.0.0

"Outside" it is sad that .net does not allow binary serialization of a class IF you are using the EXACT same DLL and assembly. (why some kind of "free" serialization method is not provided, this is beyond me, but I am not looking at us!). I mean, take byte () data - map it back to a class or struct!

Is there a way to "override" the EXACT build requirement and the wrong match problem?

Oh so simple WHEN you can share the .dll. However, I can share the source code between two applications (client and server).

Of course, the above is an assembly match.

I'm open to looking at an XML serializer (but will it pass binary data like an untouched file?). And XML serialization doesn't create a byte () array.

Is there a way to override the "fine assembly" requirement that .NET requires with binary serialization?

Vb.net code, but if a simple bit of C # code allows you to override the assembly name, I'm open to C # code.

+3


source to share


1 answer


Due to build mismatch (and wanting to avoid external dll). I would use XML Serialization. The following two routines work but increase the size of the byte () data sent by 3 times

   Public Shared Function SerializeXML(ByVal data As Object) As Byte()
        Dim ser As XmlSerializer = New XmlSerializer(data.GetType)
        Dim xBuff As New StringWriter

        ser.Serialize(xBuff, data)

        Return System.Text.Encoding.Unicode.GetBytes(xBuff.ToString)
    End Function


    Public Shared Function DeSerializeXML(Of T)(ByVal data As Byte()) As T
        Dim ser As New XmlSerializer(GetType(T))
        Dim XbUF As New MemoryStream
        XbUF.Write(data, 0, data.Length)
        XbUF.Position = 0
        Return ser.Deserialize(XbUF)
    End Function

      

So, once the data buffer () is sent over the RDP pipe, I use this:



       Dim MyEmailLoc As New oEmail

        MyEmailLoc = DeSerializeXML(Of oEmail)(MyData)

      

The above gives me the ability to send a class object over a network channel, but with significant overhead.

0


source







All Articles