Get byte[] from dataset and compress

后端 未结 2 436
感动是毒
感动是毒 2021-01-19 04:25

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

相关标签:
2条回答
  • 2021-01-19 05:06

    You can just use the following:

            using(var memory = new MemoryStream())
            {
                using(var gzip = new GZipStream(memory, CompressionMode.Compress, true))
                {
                    var formatter = new BinaryFormatter();
                    formatter.Serialize(gzip, ds);
                }
    
                return memory.ToArray();
            }
    
    0 讨论(0)
  • 2021-01-19 05:18

    There is an important step to save space:

    ds.RemotingFormat = SerializationFormat.Binary;
    

    Otherwise it internally uses xml, even via BinaryFormatter. With this in place, you can also include gzip, but the gain isn't quite as significant. As it happens I have some stats to compare this here; to copy that data in:

    DataTable (xml) (vanilla)               2269ms/6039ms
                                           64,150,771 bytes
    DataTable (xml) (gzip)                  4881ms/6714ms
                                           7,136,821 bytes
    DataTable (xml) (deflate)               4475ms/6351ms
                                           7,136,803 bytes
    BinaryFormatter (rf:binary) (vanilla)   2006ms/3366ms
                                           11,272,592 bytes
    BinaryFormatter (rf:binary) (gzip)      3332ms/4267ms
                                           8,265,057 bytes
    BinaryFormatter (rf:binary) (deflate)   3216ms/4130ms
    

    But: DataSet is not a very WCF way of doing things. I would add standard OO classes, and swap the serializer for something like protobuf-net, which is significantly smaller than either DataContractSerializer or NetDataContractSerializer.

    0 讨论(0)
提交回复
热议问题