So far we have learned that SQL Server stores sessions
exploiting the process of Serialization. Now, let us dig a bit more into it.
Serialization is noting but the act of producing an item in the form of memory
bytes.
For the process of serialization we need two things. One is
a Formatter and another is a Stream Object.
The Serialize method of the Formatter object actually does
everything that is required.
Let us see the code for this purpose. The class written below
is the class whose object is going to be serialized. For that purpose, the <Serializable()>
attribute are added.
Listing 1
<Serializable()> Public Class DataDescription
Public Data As Integer
Public Description As String
Public Sub New(ByVal NewData As Integer, ByVal NewDescription As String)
Data = NewData
Description = NewDescription
End Sub
Public Overrides Function ToString() As String
Return Description
End Function
End Class
The code below shows the actual method of Serialization.
Listing 2
Public Function SerializeData( _
ByVal oDataToBeSerialized As Object) As System.IO.MemoryStream
Dim oFormatter As New _
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim oStream As New System.IO.MemoryStream()
oFormatter.Serialize(oStream, oDataToBeSerialized)
Return oStream
End Function