Search This Blog

Showing posts with label Serialization. Show all posts
Showing posts with label Serialization. Show all posts

Saturday, September 26, 2009

Fun with Deserialization

More fun serialization, this time with deserialization.

Currently working on a .net project whose feature set will allow my to replace some older third party libraries used in a VB6 project that do not work properly under Vista 64bit edition.

Things that I have successfully completed:
  • Replaced Microsoft IE6 era Web browser control with a .net webbrowser control in a winform.
  • Replaced functionality of Microsoft Internet transfer control to download online files.
  • Successfully extended Google Picasa API and Blogger API to VB6 through COM interop type library
  • Successfully implemented binary serialization of Picasa and Blogger caching the data locally.
DRCWinformUI.DLL - COM Interop assembly exposing methods/events for use in VB6 project
PicasaBloggerUserControls.DLL - .net 2.0 framework

Now to the problem: Deserialization:

Implemented code to de-serialize objects, which works fine in .net test app, but fails over COM:
SerializationException: Cannot find assembly...

Found the following articles:
Excellent article from http://spazzarama.wordpress.com/ describing the exact same problem:

C# – BinaryFormatter.Deserialize is “Unable to find assembly”


Documentation for SerializationBinder Class

I must give props to a co-worker Tom G. who pointed me in the correct direction of properly resolving the assembly by name. A problem that was solved at work on another project.

Wednesday, September 2, 2009

Fun with Binary Serialization

Articles to read...
How to serialize an object which is NOT marked as 'Serializable' using a surrogate.
C# Tutorial - Serialize Objects to a File
Serialization in C#
Object Serialization using C#

Sample code - will serialize class with child classes that are not explicitly serializable. (Work in progress though).

SurrogateSelector ss = new SurrogateSelector();
PicasaAlbumAccessorSurrogate aass = new PicasaAlbumAccessorSurrogate();
PicasaEntrySurrogate pess = new PicasaEntrySurrogate();
PicasaFeedSurrogate pfss = new PicasaFeedSurrogate();

ss.AddSurrogate(typeof(AlbumAccessor),
new StreamingContext(StreamingContextStates.All), aass);
ss.AddSurrogate(typeof(PicasaEntry),
new StreamingContext(StreamingContextStates.All), pess);
ss.AddSurrogate(typeof(PicasaFeed),
new StreamingContext(StreamingContextStates.All), pfss);

BinaryFormatter formatter = new BinaryFormatter();
string filename = string.Format("{0}\\album_{1}_{2}_{3}.dat",
System.Environment.CurrentDirectory, _accessor.AlbumAuthor,
_accessor.Name, _accessor.Id);
FileStream fs = new FileStream(filename, FileMode.Create);
formatter.SurrogateSelector = ss;
formatter.Serialize(fs, this);
fs.Close();