开发者

Deserialize binary data in Silverlight 4

开发者 https://www.devze.com 2023-04-13 01:25 出处:网络
I thought I had read somewhere t开发者_如何学JAVAhat Silverlight 4 was going to contain a BinaryFormatter to support serializing/deserializing binary data in the client application but I can\'t seem t

I thought I had read somewhere t开发者_如何学JAVAhat 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?


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.


Are you looking for BinaryReader?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消