A
Amir Tohidi
Hi
In a nutshell: how can we produce compact persistant versions of .NET objects?
When we first started .NET development five years ago, we opted for TCP/IP
binary formatting for performance and network bandwidth reasons.
We were revisting some of this work and we noticed that our objects, when
saved using a binary formatter aren't shrinking as much as we thought.
Has something changed in .NET? For example, we expected the code below to
generate quite compact binary file, but it doesn't. And it gets worse for our
business objects (they a DataSet as their data carrier) - the binary file
contains loads of (verbose) XML!!!
using System;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace BinaryFormatting_NET1
{
[Serializable]
public class Foo : ISerializable
{
string _s;
int[] _i;
public Foo(string s)
{
_s = s;
_i = new int[1000000];
for (int c = 0; c < 1000000; c++)
_i[c] = c;
}
public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("_s", _s);
si.AddValue("_i", _i);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Foo foo = new Foo("hello");
string path =
@"W:\_Common\MiddleTierFramework\MiddleTierFrameWork.Net2.0\Tools\WinSIPPXmlCleanerTests\BinaryFormatting_NET1\foo.bin";
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Create);
bf.Serialize(fs, foo);
fs.Close();
}
}
}
In a nutshell: how can we produce compact persistant versions of .NET objects?
When we first started .NET development five years ago, we opted for TCP/IP
binary formatting for performance and network bandwidth reasons.
We were revisting some of this work and we noticed that our objects, when
saved using a binary formatter aren't shrinking as much as we thought.
Has something changed in .NET? For example, we expected the code below to
generate quite compact binary file, but it doesn't. And it gets worse for our
business objects (they a DataSet as their data carrier) - the binary file
contains loads of (verbose) XML!!!
using System;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace BinaryFormatting_NET1
{
[Serializable]
public class Foo : ISerializable
{
string _s;
int[] _i;
public Foo(string s)
{
_s = s;
_i = new int[1000000];
for (int c = 0; c < 1000000; c++)
_i[c] = c;
}
public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
si.AddValue("_s", _s);
si.AddValue("_i", _i);
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Foo foo = new Foo("hello");
string path =
@"W:\_Common\MiddleTierFramework\MiddleTierFrameWork.Net2.0\Tools\WinSIPPXmlCleanerTests\BinaryFormatting_NET1\foo.bin";
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Create);
bf.Serialize(fs, foo);
fs.Close();
}
}
}