I am returning a custom class from a WCF operation. The binding used is netTcp. This custom class contains several data members. One of them is a dataset. The dataset can be huge depending on particular actions. I am planning to compress the dataset to bytes and then return the custom class back.
Based on the reading I have come up with following code to return compressed bytes from a dataset. But not sure if this is the best way (or the correct way) to go. Your thoughts pls. ??
byte[] bytes = null;
byte[] compressedBytes = null;
using(var memory = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(memory, ds);
bytes = memory.ToArray();
}
using(var memory = new MemoryStream())
{
using(var gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(bytes, 0, bytes.Length);
compressedBytes = memory.ToArray();
}
}
return compressedBytes;