1

I thought I had read somewhere that Silverlight 4 was going to contain a BinaryFormatter to support serializing/deserializing binary data in the client application but I can't seem to locate it, so I'm guessing it's not there.

I have an existing service I need to access from my Silverlight 4 application. The service uses sockets over TCP. I've been able to get the client app connected and am able to receive messages from the service but I cannot deserialize the content of the message.

The message consists of the following object serialized on the server:

class Message
{
    String Name { get; set; }
    Stream Data { get; set; }
}

I do not have control over the service and changing the format, protocol, etc. is not an option. (Also, fwiw, Name is variable length.)

How can I reconstitute the Message object in my Silverlight client?

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
SonOfPirate
  • 5,642
  • 3
  • 41
  • 97

3 Answers3

2

I wouldn't even attempt to write binary deserialiser for Silverlight (I'm not even convinced its possible).

Instead (assuming a ASP.NET host site) I would place a WCF Service in the host site act as a kind of proxy. The WCF service will make requests to your service on behalf of the silverlight app.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • It's not an ASP.NET host site and it's a push model (the service pushes messages out to client apps, i.e. no requests). – SonOfPirate Oct 12 '11 at 16:51
0

Are you looking for BinaryReader?

Matt Bridges
  • 48,277
  • 7
  • 47
  • 61
  • I don't think so. This could help materialize an object but only if you know the exact composition of the binary data. As I mentioned, it will contain a variable length string and a variable length Stream. – SonOfPirate Oct 12 '11 at 19:30
  • 1
    How is the data serialized on the server? – Matt Bridges Oct 12 '11 at 20:05
  • As far as I can tell by reverse engineering the code, the BinaryFormatter is used to serialize the object to a stream which is then copied into the NetworkStream used by the Socket. – SonOfPirate Oct 13 '11 at 01:08
  • There isn't going to be an easy answer here -- BinaryFormatter didn't make it into SL4. – Matt Bridges Oct 13 '11 at 01:41
0

So, after much trial and error, I ended finding the following solution to my problem.

First, I was able to get access to the server code which allowed me to change the Message class so instead of the Data property returning a Stream, it returns a Byte array. I then use the XmlSerializer to serialize the object to the outgoing NetworkStream. Apparently the XmlSerializer will use Base64 encoding by default and convert the byte array to a string which can be included in the XML stream.

In the Silverlight client, I use the XmlSerializer to deserialize the byte array into the client-side object.

Not exactly the same as binary serialization, but the ultimate goal was to deserialize the binary data (byte array) received from the Socket on the SL client and this gets me there.

SonOfPirate
  • 5,642
  • 3
  • 41
  • 97