Wednesday, February 15, 2006

Beauty and Beast - Advanced Serialization

Serialization is a technique in which an object is coverted into a form that could be streamed over any Stream - this includes a FileStream and HttpWebRequest 's RequestStream etc I intentionally mentioned Request stream here. Everybody who has programmed for an year or so, uses Serialization on FileStream to save their objects, but recently i could use it differently. My requirement was that - I want to send an "object" from a windows applciation to a Remote ASPX application. I thought of Remoting the server on IIS, but it involves a new learning cycle for me as i have never worked on deploying remote objects on IIS. So this is the technique i could come up with. I opened a http request on the aspx page which accepts the object that i am going to send. On the request, i could get the request stream say "rs", then i serialized my object on the response stream using BinaryFormatter. That is it. But the serialized data to be streamed, you should also fetch the response, only when you call the GetResponse() your data would be actually streamed.

In the aspx page, i created another BinaryFormatter in the Page Preload event handler. There i fetched the request object and the base stream inside it, Then i called the Deserialized over it to get back my object.

This was really cool way to establish communication between a windows application and web application. If it was XML serialization , then probably i could even communicate with a servlet or a JSP container. I did not try it out yet, but if anyone does that, post a comment over this blog.

I had another requirement where I used serialization again, but here the requirement was that the object had some not so secret, but information that my client wanted to secure. So i thought of encrypted serialzation and i have found a good article whose link i posted it over here. But somehow i could not implement it successfully. Then i thought if data is compressed, it is not human readable in any text reader like notepad or wordpad. So i thought of using the compressed serialization as in the link i gave. But GZipCompression did not work for me and i had to go for DeflateCompression method. I created a compression stream over the filestream and i used it for serialization. The important point is htat deserialization shall not work unless you add the Flush() statment after the serialization is performed. I shall add the code block, in the next blog.

The applicaitons of Serialization is not well covered anywhere in the documentation. It could be well used, and its so beautiful. But hey! there are numerous issues you shall face during deserialization. As i get more acquainted with blogging, i shall make this blog a better and excellent place for information on .NET and sometimes Java. I love bill Gates, just because he gave us .NET platform.(ofcourse i would have loved Jennifer Lopez, had her company come up with something as good as .NET)

No comments: