Serialization – Pitney Bowes MapXtreme User Manual

Page 200

Advertising
background image

Chapter 9: Working with Core MapXtreme Classes

Serialization and Persistence

MapXtreme v7.1

207

Developer Guide

Serialization

Serialization is the process of converting an object into a stream of data in order to preserve it on the
server. This process is an essential part of maintaining objects in MapXtreme web applications. If the
objects are not maintained the server would need to recreate the object (such as a map) for each
web request. When an object is requested, it is deserialized (or recreated from the stream of data)
and then modified. This algorithm does not make a copy of the object (as other serialization
algorithms do) such that the object being deserialized is created only once.

The serialization is performed by formatters that are embedded in the Microsoft.NET Framework.
Two different formatters are included in the Framework, one for binary objects, BinaryFormatter, and
one for SOAP objects, SOAPFormatter (SOAP is a lightweight protocol intended for exchanging
structured information in a distributed environment, such as the web.). SOAPFormatter is relatively
faster than BinaryFormatter. SOAPFormatter is used for certain basic types of data (Int, Byte,
Decimal, etc.) and BinaryFormatter is called for complex of objects. See the Microsoft MSDN
documentation for more information about the SOAPFormatter and the BinaryFormatter.

To pass an object to one of these formatters, use the GetObjectData() method. To deserialize the
object (restore it from the stream) use the SetObjectData() method.

Any object that supports the ISerializable interface will automatically be restored, or deserialized.
The ASP.NET framework automatically deserializes the context.Session[] array after
HttpApplication.BeginRequest. MapInfo.Engine.Session is set up in HttpApplication.BeginRequest
handler so objects are deserialized into the MapInfo.Engine.Session.

Serialize/Deserialize the Session Object

The following is an example of how to serialize/deserialize the Session object.

// Create a MemoryStream to serialize into
MemoryStream stream = new MemoryStream();
// Serialize the MapXtreme Session object
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, Session.Current);
stream.Position = 0;
// Make changes to the Session object to make sure the
// deserialization works correctly
...
// Recreate the MapXtreme Session object from the stream
// Note: this will replace the current MapXtreme Session object with the
// contents of the stream
formatter = new BinaryFormatter();
formatter.Deserialize(stream);

The stream parameter passed to the formatter.Serialize method can be anything derived from
System.IO.Stream.

This serialization functionality was designed to be used with MapXtreme’s state management
feature. If you persist this information to disk, then try to reload it in the future with a different version
of the product, it's not guaranteed to work.

For more on serialization and state management see

Implementing a StateManager

.

Advertising